????????最近在項目中用到了個推來實現消息推送,我的功能比較簡單,調用系統的通知欄展現我的推送消息就可以了,我就直接貼代碼了,前面的注冊登錄啊什么的就省略了。我本節的重點是在我們接收到個推消息怎么處理,而前面的代碼我就完全用了個推的demo,我會上傳我的demo附件,這里面大家直接從個推官網登記一個應用換上自己應用對應的
appid,appkey,appSecret就可以用在個推官網進行相應的透傳推送測試了。
1.我推薦大家使用AndroidStudio快速集成,官網上面有詳細的文檔來支撐。
2.初始化個推sdk,這些照著官方的文檔來弄就好了,初始化的地方需要注意,最好是在住Activity來初始化,不要在MainApplication初始化。我前面是在MainApplication初始化的,但是當我啟動APP的時候看見我的控制臺總是打印了兩邊初始化日志,我百思不得其姐,為什么呢,重點來了,原來個推sdk是一個獨立的進程,應用在沒啟動一個進程的時候就會調用MainApplication的oncreate方法,因此我的控制臺打印了兩次。所以個推也是建議在主Activity中調用。
PushManager.getInstance().initialize(this.getApplicationContext(),userPushService);
// 注冊 intentService 后 PushDemoReceiver 無效, sdk 會使用 DemoIntentService 傳遞數據,
// AndroidManifest 對應保留一個即可(如果注冊 DemoIntentService, 可以去掉 PushDemoReceiver, 如果注冊了
// IntentService, 必須在 AndroidManifest 中聲明)
PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
3.個推推送過來的消息在DemoIntentService這個類中的onReceiveMessageData這個方法中可以接受到,后續我們就處理我們自己透傳邏輯了。
4.我需要將服務端發來的透傳消息展示在系統通知欄,我不需要自定義通知欄,只需調用系統的就可以這其中主要使用到了兩個NotificationCompat系統類和notificationManager,下面是核心代碼,在網上搜一下一大堆,重點是我們要注意到messagid,我們每條消息的messageid都不能相同,否則會導致前面的消息被最新的消息覆蓋,這也是我查了好久才發現的坑,希望大家注意一下。而PendingIntent測試定義通知欄的點擊事件
//定義廣播接收器
//點擊通知欄啊
Intent intentClick =newIntent(context, PushReceiver.class);
intentClick.setAction("notification_clicked");
intentClick.putExtra(PushReceiver.TYPE,type);
intentClick.putExtra("obj",jsonObject.toString());
intentClick.putExtra("a",messageId);
PendingIntent pendingIntentClick = PendingIntent.getBroadcast(context,messageId, intentClick, PendingIntent.FLAG_UPDATE_CURRENT);//flag應設置為FLAG_UPDATE_CURRENT否則只有一次點擊事件
//清除
Intent intentCancel =newIntent(context, PushReceiver.class);
intentCancel.setAction("notification_cancelled");
intentCancel.putExtra(PushReceiver.TYPE,type);
PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(context,messageId, intentCancel, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder =newNotificationCompat.Builder(context);
builder.setContentTitle(title);//設置標題
builder.setContentText(content);//設置內容
builder.setSmallIcon(R.drawable.push_small);//設置推送的圖片
builder.setShowWhen(true);//設置顯示時間
builder.setOngoing(false);//是否可手動消除改通知
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_VIBRATE);
builder.setPriority(Notification.PRIORITY_DEFAULT);
builder.setContentIntent(pendingIntentClick);
builder.setDeleteIntent(pendingIntentCancel);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);//通知管理器
notificationManager.notify(messageId,builder.build());
5.在PushReceiver中實現對通知欄點擊事件的實現
public voidonReceive(Context context, Intent intent) {
//TODO: This method is called when the BroadcastReceiver is receiving
String action = intent.getAction();
//點擊
if(action.equals("notification_clicked")) {
//處理點擊事件
System.out.println(" 點擊了"+intent.getStringExtra("obj"));
obj= intent.getStringExtra("obj");
inta = intent.getIntExtra("a",-1);
if(a != -1) {
NotificationManager notifiMgr = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
System.out.println("點擊了"+a);
notifiMgr.cancel(a);
}
if(obj!=null){
//將數據回傳給js端
}
}
//刪除,取消
if(action.equals("notification_cancelled")) {
//處理滑動清除和點擊刪除事件
System.out.println("刪除了");
}
}
這樣就完成了一個簡單的透傳消息處理,點擊后我們需要做后續的處理,跳轉到某個具體的activity,由于我用的是hbuilder的離線打包框架,我還需要將數據回踢到js端,如果有興趣可以和我qq:815186911,這是demo的百度云鏈接 :http://pan.baidu.com/s/1mioWhfi