同时显示多个Notification时PendingIntent的Intent被覆盖?

http://univasity.iteye.com/blog/1390445

情况是这样的,使用NotificationManager触发多个Notification:

Java代码   收藏代码
  1. private Notification genreNotification(Context context, int icon, String tickerText, String title, String content, Intent intent){  
  2.         Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());  
  3.         PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  4.         notification.setLatestEventInfo(context, title, content, pendIntent);  
  5.         notification.flags |= Notification.FLAG_AUTO_CANCEL;  
  6.         return notification;  
  7.     }  
  8.   
  9. ...  
  10. mNotificationManager.notify(ID_1,   
  11.                     genreNotification(mContext, ICON_RES,   
  12.                             notifyText1, notifyTitle1, notifyText1, intent_1));  
  13. ...  
  14. mNotificationManager.notify(ID_2,   
  15.                     genreNotification(mContext, ICON_RES,   
  16.                             notifyText2, notifyTitle2, notifyText2, intent_2));  
  17.   
  18. ...  
  19. mNotificationManager.notify(ID_3,   
  20.                     genreNotification(mContext, ICON_RES,   
  21.                             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代码   收藏代码
  1. private Notification genreNotification(Context context, int icon, String tickerText, String title, String content, Intent intent, int id){  
  2.         Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());  
  3.         // 问题就在这里的id了  
  4.         PendingIntent pendIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  5.         notification.setLatestEventInfo(context, title, content, pendIntent);  
  6.         notification.flags |= Notification.FLAG_AUTO_CANCEL;  
  7.         return notification;  
  8.     }  
  9.   
  10. ...  
  11. mNotificationManager.notify(ID_1,   
  12.                     genreNotification(mContext, ICON_RES,   
  13.                             notifyText1, notifyTitle1, notifyText1, intent_1, ID_1));  
  14. ...  
  15. mNotificationManager.notify(ID_2,   
  16.                     genreNotification(mContext, ICON_RES,   
  17.                             notifyText2, notifyTitle2, notifyText2, intent_2, ID_2));  
  18.   
  19. ...  
  20. mNotificationManager.notify(ID_3,   
  21.                     genreNotification(mContext, ICON_RES,   
  22.                             notifyText3, notifyTitle3, notifyText3, intent_3, ID_3));  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值