推送:腾迅信鸽 VS Bmob
时间:2022-04-27 18:57
最近几天了解市场上主流的推送SDK。
腾迅信鸽
所需SDK,去官网自行下载。
完整的清单文件如下:
1 2View Code6 7 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 35 110 11138 44 4539 4340 41 42 48 72 73 74 7549 50 51 62 6352 53 54 55 56 57 58 59 60 61 64 7165 66 67 68 69 70 78 84 85 8679 80 81 8382 91 92 93 96 100 101 10297 9998 105 106 109
MainActivity中配置如下:
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 XGPushManager.registerPush(this); 6 // // 2.36(不包括)之前的版本需要调用以下2行代码 7 // Intent service = new Intent(this, XGPushService.class); 8 // startService(service); 9 }View Code
相当Easy吧,如果过程无误,就可以正常推送了。
Bmob
Bmob配置过程相对信鸽来说稍微复杂一点,缺点就是需要自己在自定义Receiver里接收推送消息,自己发送通知,但可定制性也较强。
所需SDK,去官网自行下载。
完整的清单文件如下:
1 2View Code6 7 10 11 12 15 16 1718 19 20 21 22 23 24 25 26 27 28 29 34 73 7438 44 4539 4340 41 42 51 55 5652 5453 57 66 6758 59 6560 61 62 63 64 68 7269 7170
MainActivity中配置如下:
1 // 初始化BmobSDK 2 Bmob.initialize(this, "你的AppKey"); 3 // 使用推送服务时的初始化操作 4 BmobInstallation.getCurrentInstallation(this).save(); 5 // 启动推送服务 6 BmobPush.startWork(this, "你的AppKey");View Code
MyPushMessageReceiver完整代码如下:
1 public class MyPushMessageReceiver extends BroadcastReceiver { 2 3 private Context mContext; 4 5 @Override 6 public void onReceive(Context context, Intent intent) { 7 mContext = context; 8 if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) { 9 String jsonstr = intent 10 .getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING); 11 String msg = ""; 12 try { 13 JSONObject object = new JSONObject(jsonstr); 14 msg = object.getString("alert"); 15 } catch (JSONException e) { 16 e.printStackTrace(); 17 } 18 19 SendNotification(msg); 20 } 21 } 22 23 /** 24 * 发送通知 25 * 26 * @param message 27 */ 28 @SuppressWarnings("deprecation") 29 private void SendNotification(String message) { 30 // 点击之后执行的Intent 31 Intent intent = new Intent(mContext, MainActivity.class); 32 PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, 33 intent, 0); 34 Notification notification = new Notification(); 35 notification.icon = R.drawable.ic_launcher; 36 notification.tickerText = "收到消息推送"; 37 notification.when = System.currentTimeMillis(); 38 notification.defaults = Notification.DEFAULT_SOUND 39 | Notification.DEFAULT_VIBRATE;// 设置默认为系统声音 40 notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击后自动消失 41 notification.setLatestEventInfo(mContext, "推送消息", message, 42 pendingIntent); 43 NotificationManager mManager = (NotificationManager) mContext 44 .getSystemService(Context.NOTIFICATION_SERVICE); 45 mManager.notify(1, notification); 46 47 } 48 }View Code