情况是这样的,使用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));

[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?的更多相关文章

  1. js倒计时,显示NaN天NaN时NaN分(或显示天时分)

    最近在开发跨平台的应用,在做秒杀功能时,倒计时出现了问题.默认在Chrome浏览器中运行,倒计时没出现问题.而在IE浏览器,火狐浏览器,safari浏览器上运行时,则显示NaN天NaN时NaN分(或显 ...

  2. 五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时、新增加子控件时、重新创建当前控件的句柄时、设置父控件时、显示状态被改变时

    五种情况下会刷新控件状态(刷新控件状态才能刷新所有子FWinControls的显示): 在TWinControls.PaintControls中,对所有FWinControls只是重绘了边框,而没有整 ...

  3. 同时显示多个 Notification

    主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数(给出的例子也直接写0的),实际上是通过该参数来区别不同的Intent的,如果id相同, ...

  4. Android 问题解决 HorizontalScrollView显示不全(转)

    原链接:https://www.jianshu.com/p/003adbcaff9d Android 问题解决 HorizontalScrollView显示不全 <HorizontalScrol ...

  5. Notification(二)——PendingIntent的flag导致数据同样的问题

    MainActivity例如以下: package cc.cu; import android.os.Bundle; import android.view.View; import android. ...

  6. 轻松让HTML5可以显示桌面通知Notification非常实用

    使用Notification的流程 1.检查浏览器是否支持Notification2.检查浏览器的通知权限3.如果权限不够则申请获取权限4.创建消息通知5.展示消息通知 Notification AP ...

  7. iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项

    我们在开发中会遇到这样的情况:调用键盘时需要界面有一个调整,避免键盘遮掩输入框. 但实现时你会发现,在不同的手机上键盘的高度是不同的.这里列举一下: //获取键盘的高度 /* iphone 6: 中文 ...

  8. webpages框架中使用Html.TextArea()在前台显示多行信息时,如何进行大小、样式的设置

    环境:vs2015 webpages框架+razor语法: 目的:服务器进行数据更新操作后,在前台显示更新的相关信息: 后台代码:将更新条数等相关信息存储在一个变量中: @{ var serverIn ...

  9. Delphi: TLabel设置EllipsisPosition属性用...显示过长文本时,以Hint显示其全文本

    仍然是处理多语言中碰到问题. Delphi自2006版以后,TLabel有了EllipsisPosition属性,当长文本超过其大小时,显示以...,如下图: 这样虽然解决显示问题,但很显然,不知道. ...

随机推荐

  1. Zju1610 Count the Colors

    题面: 画一些颜色段在一行上,一些较早的颜色就会被后来的颜色覆盖了. 你的任务就是要数出你随后能看到的不同颜色的段的数目. Input: 每组测试数据第一行只有一个整数n, 1 <= n < ...

  2. 【agc006f】Blackout(神仙题)

    [agc006f]Blackout(神仙题) 翻译 给定一个\(n*n\)的网格图,有些格子是黑色的.如果\((x,y),(y,z)\)都是黑色的,那么\((y,x)\)也会被染黑,求最终黑格子数量. ...

  3. 【BZOJ1054】移动玩具(搜索)

    [BZOJ1054]移动玩具(搜索) 题面 BZOJ 洛谷 题解 这种小清新搜索题写出来好舒服啊. 要是原来的我来写代码肯定又臭又长吧.. #include<cstdio> #includ ...

  4. Android 资源目录 相关知识 raw、 drawable、 values..

    一定要看的 Android 资源目录的相关知识 raw drwable valueshttp://blog.csdn.net/shichexixi/article/details/5985677 ht ...

  5. Java入门:基础算法之检查奇偶性

    本程序检查一个数是奇数还是偶数. import java.util.Scanner; class CheckEvenOdd { public static void main(String args[ ...

  6. shell实例浅谈之一产生随机数七种方法

    一.问题 Shell下有时需要使用随机数,在此总结产生随机数的方法.计算机产生的的只是“伪随机数”,不会产生绝对的随机数(是一种理想随机数).伪随机数在大量重现时也并不一定保持唯一,但一个好的伪随机产 ...

  7. 使用uwsgi配置django

    1.uwsgi的安装 pip install uwsgi 2.uwsgi的基本测试: 创建一个test.py文件,内容如下: def application(env, start_response): ...

  8. Hadoop生态圈-Hbase的rowKey设计原则

    Hadoop生态圈-Hbase的rowKey设计原则 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  9. 豆瓣电影api

    1.获取正在热映的电影: 接口:https://api.douban.com/v2/movie/in_theaters   访问参数: start : 数据的开始项 count:单页条数 city:城 ...

  10. 让你快速学会Shell脚本

    Shell脚本,就是利用Shell的命令解释的功能,对一个纯文本的文件进行解析,然后执行这些功能,也可以说Shell脚本就是一系列命令的集合.Shell可以直接使用在win/Unix/Linux上面, ...