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

  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. mNotificationManager.notify(ID_1,
  10. genreNotification(mContext, ICON_RES,
  11. notifyText1, notifyTitle1, notifyText1, intent_1));
  12. ...
  13. mNotificationManager.notify(ID_2,
  14. genreNotification(mContext, ICON_RES,
  15. notifyText2, notifyTitle2, notifyText2, intent_2));
  16. ...
  17. mNotificationManager.notify(ID_3,
  18. genreNotification(mContext, ICON_RES,
  19. 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参数):

  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. mNotificationManager.notify(ID_1,
  11. genreNotification(mContext, ICON_RES,
  12. notifyText1, notifyTitle1, notifyText1, intent_1, ID_1));
  13. ...
  14. mNotificationManager.notify(ID_2,
  15. genreNotification(mContext, ICON_RES,
  16. notifyText2, notifyTitle2, notifyText2, intent_2, ID_2));
  17. ...
  18. mNotificationManager.notify(ID_3,
  19. genreNotification(mContext, ICON_RES,
  20. notifyText3, notifyTitle3, notifyText3, intent_3, ID_3));

最新文章

  1. Java日期时间操作的一些方法
  2. Windows下QT Creator工程中添加文件夹
  3. 浅谈Extjs radiogroup change事件与items下的checked属性
  4. js one() 方法
  5. AngularJS中的按需加载ocLazyLoad
  6. JSON 问题
  7. MySQL 通过idb文件恢复Innodb 数据【转】
  8. javascript 调试技巧
  9. php反射应用实例代码
  10. SBM is Not Sale And Run Company
  11. php cloure闭包
  12. python中的那些“神器”
  13. [BZOJ 1058][ZJOI 2007]报表统计 平衡树+线段树
  14. ubuntu 14.04中安装phpmyadmin即mysql图形管理界面
  15. js 原型 对象篇
  16. Linux编程 2 (遍历目录cd与查看文件和目录ls)
  17. CSS3 transition 属性过渡效果 详解
  18. Eclipse配置方法注释模板
  19. System.ConfigurationManager类用于对配置文件的读取
  20. PHP中header的用法总结

热门文章

  1. 【比赛】NOIP2017 宝藏
  2. 【BZOJ1048】分割矩阵(记忆化搜索,动态规划)
  3. 【转】VMware虚拟机三种网络模式详解
  4. 【bzoj3573】 Hnoi2014—米特运输
  5. UOJ #126 【NOI2013】 快餐店
  6. 响应式开发(五)-----Bootstrap CSS----------Bootstrap 网格系统
  7. 如何在 Android 程序中禁止屏幕旋转和重启Activity
  8. 解题:CF1009 Dominant Indices
  9. 2017易观OLAP算法大赛
  10. Docker集群管理Swarm数据持久化