情况是这样的,使用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. 【刷题】BZOJ 3529 [Sdoi2014]数表

    Description 有一张n×m的数表,其第i行第j列(1<=i<=n,1<=j<=m)的数值为能同时整除i和j的所有自然数之和.给定a,计算数表中不大于a的数之和. In ...

  2. 【BZOJ4709】【Jsoi2011】柠檬

    Description 传送门 题意简述:将序列划分成任意多段,从每一段选出一个数\(x\),获得\(在这一段出现的次数x*(x在这一段出现的次数)\)的贡献.求总贡献最大值. Solution ​ ...

  3. 前端学习 -- Css -- 伪类

    伪类和伪元素用来表示元素所处的一个特殊的状态,或者是一个特殊的位置 比如:访问过的超链接,比如普通的超链接,比如获取焦点的文本框当我们需要为处在这些特殊状态的元素设置样式时,就可以使用伪类 为没访问过 ...

  4. java 批量文件后缀重命名

    import java.io.File; public class BatchFileSuffixRename { public static void main(String[] args) { / ...

  5. js中apply(thisArg, [argsArray])的参数与ArrayLike的关系

    你是否写过或见到过这样的代码 xx.apply(this,slice.call(arguments)) //slice.call转为数组是否多余 mdn地址 msdn地址 一.微软和mdn对参数的介绍 ...

  6. 解题:APIO/CTSC 2007 数据备份

    题面 用双向链表把相邻两项的差串起来,用大根堆维护价值,每次贪心取最大的$x$.取完之后打标记删掉$pre[x]$和$nxt[x]$,之后用$val[pre[x]]+val[nxt[x]]-val[x ...

  7. Html 响应式 Web

    网格视图 很多网页都是基于网格设计的,这说明网页是按列来布局的. 使用网格视图有助于我们设计网页.这让我们向网页添加元素变的更简单. 响应式网格视图通常是 12 列,宽度为100%,在浏览器窗口大小调 ...

  8. C - Ilya And The Tree Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...

  9. laravel 数据库迁移转 sql 语句

    可以使用下面的命令 php artisan migrate --pretend --no-ansi 当然,你需要有可以 migrate 的东西. 数据库迁移导出到文件(使用命令) <?php n ...

  10. Java并发编程原理与实战十二:深入理解volatile原理与使用

    volatile:称之为轻量级锁,被volatile修饰的变量,在线程之间是可见的. 可见:一个线程修改了这个变量的值,在另一个线程中能够读取到这个修改后的值. synchronized除了线程之间互 ...