[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
情况是这样的,使用NotificationManager触发多个Notification:
- 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参数):
- 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());
- // 问题就在这里的id了
- 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));
[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?的更多相关文章
- js倒计时,显示NaN天NaN时NaN分(或显示天时分)
最近在开发跨平台的应用,在做秒杀功能时,倒计时出现了问题.默认在Chrome浏览器中运行,倒计时没出现问题.而在IE浏览器,火狐浏览器,safari浏览器上运行时,则显示NaN天NaN时NaN分(或显 ...
- 五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时、新增加子控件时、重新创建当前控件的句柄时、设置父控件时、显示状态被改变时
五种情况下会刷新控件状态(刷新控件状态才能刷新所有子FWinControls的显示): 在TWinControls.PaintControls中,对所有FWinControls只是重绘了边框,而没有整 ...
- 同时显示多个 Notification
主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数(给出的例子也直接写0的),实际上是通过该参数来区别不同的Intent的,如果id相同, ...
- Android 问题解决 HorizontalScrollView显示不全(转)
原链接:https://www.jianshu.com/p/003adbcaff9d Android 问题解决 HorizontalScrollView显示不全 <HorizontalScrol ...
- Notification(二)——PendingIntent的flag导致数据同样的问题
MainActivity例如以下: package cc.cu; import android.os.Bundle; import android.view.View; import android. ...
- 轻松让HTML5可以显示桌面通知Notification非常实用
使用Notification的流程 1.检查浏览器是否支持Notification2.检查浏览器的通知权限3.如果权限不够则申请获取权限4.创建消息通知5.展示消息通知 Notification AP ...
- iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项
我们在开发中会遇到这样的情况:调用键盘时需要界面有一个调整,避免键盘遮掩输入框. 但实现时你会发现,在不同的手机上键盘的高度是不同的.这里列举一下: //获取键盘的高度 /* iphone 6: 中文 ...
- webpages框架中使用Html.TextArea()在前台显示多行信息时,如何进行大小、样式的设置
环境:vs2015 webpages框架+razor语法: 目的:服务器进行数据更新操作后,在前台显示更新的相关信息: 后台代码:将更新条数等相关信息存储在一个变量中: @{ var serverIn ...
- Delphi: TLabel设置EllipsisPosition属性用...显示过长文本时,以Hint显示其全文本
仍然是处理多语言中碰到问题. Delphi自2006版以后,TLabel有了EllipsisPosition属性,当长文本超过其大小时,显示以...,如下图: 这样虽然解决显示问题,但很显然,不知道. ...
随机推荐
- 迭代解析JSON简单实例
由于项目中遇到了这个问题,所以在这里记录一下. 比如:请求到的JSON串: { "msg":"数据获取成功", "success":true ...
- Fibonacci数列时间复杂度之美妙
Fibonacci数列: fib(0)=1 fib(1)=1 fib(n)=fib(n-1)+fib(n-2) 上课老师出了一道题,求下列函数的时间复杂度: int fib(int d) { ) ; ...
- [poj 1533]最长上升子序列nlogn树状数组
题目链接:http://poj.org/problem?id=2533 其实这个题的数据范围n^2都可以过,只是为了练习一下nlogn的写法. 最长上升子序列的nlogn写法有两种,一种是变形的dp, ...
- git "refusing to merge unrelated histories" 解决方法
出现这个错误是因为本地的 git 历史和远程仓库的 git 历史不一样,如果我们想要合并两个不同的 git 历史(我们必须要清楚我们在做什么),就可以使用这个选项来进行强制合并不同的 git 历史,如 ...
- mysql 优化总结
1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. (laravel 可以使用 debugbar 包,可以及时发现低效 sql 语句,不使用索 ...
- Netflix的zuul使用
1.zuul出现的原因 2.zuul的介绍 3.zuul如何使用 4.zuul的一些注意事项
- 2018java面试集合
作者:刘成链接:https://www.zhihu.com/question/266822548/answer/317700943来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- 【题解】Tree-String Problem Codeforces 291E AC自动机
Prelude 传送到Codeforces:(/ω\)--- (/ω•\) Solution 很水的一道题. 对查询的串建出来AC自动机,然后树上随便跑跑就行了. 为什么要写这篇题解呢? 我第一眼看到 ...
- angularjs的Controller as
<html ng-app="notesApp"> <head><title>Notes App</title></head&g ...
- codevs 3160 最长公共子串
3160 最长公共子串 http://codevs.cn/problem/3160/ 时间限制: 2 s 空间限制: 128000 KB 题目描述 Description 给出两个由小写字母组 ...