情況是這樣的,使用NotificationManager觸發多個Notification:
Java代碼 ?
private?Notification?genreNotification(Context?context,?int?icon,?String?tickerText,?String?title,?String?content,?Intent?intent){ ??????????Notification?notification?=?new?Notification(icon,?tickerText,?System.currentTimeMillis()); ??????????PendingIntent?pendIntent?=?PendingIntent.getActivity(context,?0,?intent,?PendingIntent.FLAG_UPDATE_CURRENT); ??????????notification.setLatestEventInfo(context,?title,?content,?pendIntent); ??????????notification.flags?|=?Notification.FLAG_AUTO_CANCEL; ??????????return?notification; ??????} ????... ??mNotificationManager.notify(ID_1,? ??????????????????????genreNotification(mContext,?ICON_RES,? ??????????????????????????????notifyText1,?notifyTitle1,?notifyText1,?intent_1)); ??... ??mNotificationManager.notify(ID_2,? ??????????????????????genreNotification(mContext,?ICON_RES,? ??????????????????????????????notifyText2,?notifyTitle2,?notifyText2,?intent_2)); ????... ??mNotificationManager.notify(ID_3,? ??????????????????????genreNotification(mContext,?ICON_RES,? ??????????????????????????????notifyText3,?notifyTitle3,?notifyText3,?intent_3));?? private Notification genreNotification(Context context, int icon, String tickerText, String title, String content, Intent intent){Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);notification.setLatestEventInfo(context, title, content, pendIntent);notification.flags |= Notification.FLAG_AUTO_CANCEL;return notification;}...
mNotificationManager.notify(ID_1, genreNotification(mContext, ICON_RES, notifyText1, notifyTitle1, notifyText1, intent_1));
...
mNotificationManager.notify(ID_2, genreNotification(mContext, ICON_RES, notifyText2, notifyTitle2, notifyText2, intent_2));...
mNotificationManager.notify(ID_3, genreNotification(mContext, ICON_RES, notifyText3, notifyTitle3, notifyText3, intent_3));
?可見ID和Intent都是不同的,生成的PendingIntent分別對應著不同的Intent。但是,你會發覺無論點哪個Notification,傳遞回來的都是最后被notify的Intent。這里即intent_3。
?
找了很久,試了改變PendingIntent的flag也無果,最后還是在這帖子里找到答案(CSDN帖子 ),我來總結下:
問題主要出在PendingIntent.getActivity();的第二個參數,API文檔里雖然說是未被使用的參數(給出的例子也直接寫0的),實際上是通過該參數來區別不同的Intent的,如果id相同,就會覆蓋掉之前的Intent了。所以總是獲取到最后一個Intent。
?
只要每個不同的Intent對應傳遞一個獨立的ID就可以了,以上函數修改如下(增加ID參數):
Java代碼 ?
private?Notification?genreNotification(Context?context,?int?icon,?String?tickerText,?String?title,?String?content,?Intent?intent,?int?id){ ??????????Notification?notification?=?new?Notification(icon,?tickerText,?System.currentTimeMillis()); ??????????????????PendingIntent?pendIntent?=?PendingIntent.getActivity(context,?id,?intent,?PendingIntent.FLAG_UPDATE_CURRENT); ??????????notification.setLatestEventInfo(context,?title,?content,?pendIntent); ??????????notification.flags?|=?Notification.FLAG_AUTO_CANCEL; ??????????return?notification; ??????} ????... ??mNotificationManager.notify(ID_1,? ??????????????????????genreNotification(mContext,?ICON_RES,? ??????????????????????????????notifyText1,?notifyTitle1,?notifyText1,?intent_1,?ID_1)); ??... ??mNotificationManager.notify(ID_2,? ??????????????????????genreNotification(mContext,?ICON_RES,? ??????????????????????????????notifyText2,?notifyTitle2,?notifyText2,?intent_2,?ID_2)); ????... ??mNotificationManager.notify(ID_3,? ??????????????????????genreNotification(mContext,?ICON_RES,? ??????????????????????????????notifyText3,?notifyTitle3,?notifyText3,?intent_3,?ID_3));??
轉載于:https://www.cnblogs.com/wangluochong/p/4189716.html
總結
以上是生活随笔為你收集整理的[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。