您的位置:首页 > 博客中心 > 互联网 >

推送:腾迅信鸽 VS Bmob

时间:2022-04-27 18:57

最近几天了解市场上主流的推送SDK。

腾迅信鸽

所需SDK,去官网自行下载。

技术分享

 完整的清单文件如下:

技术分享技术分享
  1 
  2 
  6 
  7     
 10     
 11     
 12     
 13     
 14     
 15     
 16     
 17     
 18     
 19     
 20     
 21     
 22     
 23     
 24     
 25     
 26     
 27     
 28     
 29 
 30     
 35         
 38             
 39                 
 40 
 41                 
 42             
 43         
 44         
 45         
 48             
 49 
 50                 
 51                 
 52                 
 53                 
 54                 
 55                 
 56 
 57                 
 58                 
 59                 
 60                 
 61             
 62             
 63             
 64                 
 65                 
 66                 
 67                 
 68 
 69                 
 70             
 71         
 72 
 73         
 74         
 75         
 78             
 79 
 80                 
 81                 
 82             
 83         
 84 
 85         
 86         
 91 
 92         
 93         
 96             
 97                 
 98             
 99         
100 
101         
102         
105         
106         
109     
110 
111 
View Code

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 
 2 
 6 
 7     
10 
11     
12     
15     
16 
17      
18     
19     
20     
21     
22     
23     
24     
25     
26     
27     
28 
29     
34         
38             
39                 
40 
41                 
42             
43         
44 
45         
51             
52                 
53             
54         
55 
56         
57              
58                 
59                 
60                 
61                 
62                 
63                 
64             
65         
66         
67         
68             
69                 
70             
71         
72     
73 
74 
View Code

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

 

本类排行

今日推荐

热门手游