最近使用notification,对多个notification引发的问题总结如下(只是我碰到的):

1.多个通知间,点其中一个可能取到另一个通知的内容(通知的添加的代码相似)

2.多个通知间,点击完其中一个通知,导致另外一个通知无法点击

下面贴上自己的一段测试代码:

public class NotificationActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button1); EditText editText = (EditText) findViewById(R.id.editText1); editText.setHeight(100); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub addNotification(2); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } addNotification(1); } }); } /** * 添加一个notification */ private void addNotification(int id){ NotificationManager nm = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon, "hello,here", System.currentTimeMillis()); notification.flags = Notification.FLAG_AUTO_CANCEL; Intent intent = new Intent(this, Notification2Activity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("id", id + ""); PendingIntent contentIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(this, "有新消息", id + "", contentIntent); nm.notify(id, notification); } }

public class Notification2Activity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main2); Log.i("Notification2Activity 收到id:", getIntent().getStringExtra("id")); } }

这是我测试通过,没问题后的代码.

现在说说上述两个问题的症状在哪?

1.PendingIntent.FLAG_UPDATE_CURRENT,如果设置PendingIntent.FLAG_ONE_SHOT,PendingIntent 紧紧只能被点击一次.

PendingIntent.FLAG_UPDATE_CURRENT具体代表啥,去看源码吧.

2.

public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) { String packageName = context.getPackageName(); String resolvedType = intent != null ? intent.resolveTypeIfNeeded( context.getContentResolver()) : null; try { IIntentSender target = ActivityManagerNative.getDefault().getIntentSender( IActivityManager.INTENT_SENDER_ACTIVITY, packageName, null, null, requestCode, intent, resolvedType, flags); return target != null ? new PendingIntent(target) : null; } catch (RemoteException e) { } return null; }

这是获取PendingIntent的源码,

第二个参数requestCode,代表着发送者的一个标识,用来区分不同通知的接收者.

所以这地方需要设置下,不能相同,否则产生前面提到的第一个的问题

多个notification引发的问题相关推荐

  1. mongodb java 日志分析_记一次log4j与mongodb集成引发的问题分析

    问题背景 对项目中的关键应用调用链日志需要结构化得统一吐出到mongodb中,同时项目中日志输出使用log4j,故准备使用log4j的Appender直接集成mongodb的输出,同时mongodb采 ...

  2. Swift使用通知Notification

    通知简介 通知(Notification)是观察者模式(observer)中的一种, 结构为: 发布者 -> 通知中心 -> 接收者, 是iOS 开发中常用通信方式,为单例模式使用.可以实 ...

  3. OMA 设备管理的通知发起的会话OMA Device Management Notification Initiated Session

    介绍 一些设备不能持续侦听是否有需要连接的服务器.有的设备基于安全考虑不希望"开放一个端口"(比如主动接受连接).无论如何,大多数设备可以接受未经请求的信息,有时也叫做" ...

  4. 马宁的Windows Phone 7.1初体验(二)——Push Notification

    Push Notification并不是Windows Phone 7.1的新功能,但是之前的文章里对这部分都缺少详细的分析,所以姑且就把Push Notification放到这部分里吧. 很多iOS ...

  5. /var/lib/docker/overlay2/xxxxx no such file or directory docker文件删除引发的问题

    记一次误删引发的服务雪崩 K8s node节点磁盘报警,报警后我找到服务中占用磁盘最多的地方,在overlay2目录下,对下面的文件进行了删除   删除后,有状态服务先出现了问题,服务无法启动.停止. ...

  6. Notification 使用详解

    录制了一个gif 图大家看看效果 由于手机厂商修改问题,这个显示可能存在差役,但是这个提示框都是会显示的,运行是在android 7 8 ,9 三个版本运行的都没有问题 下面开始介绍它的使用 Noti ...

  7. 由Node.js事件驱动模型引发的思考

    引言 近段时间听说了Node.js,很多文章表述这个事件驱动模型多么多么优秀,应用在服务器开发中有很大的优势,本身对此十分感性去,决定深入了解一下,由此也引发了一些对程序设计的思考,记录下来. 什么是 ...

  8. Notification和KVO有什么不同

    Notification是推送通知,我们可以建立一个通知中心,存放创建多个通知,在不同的地方在需要的时候push调用 和KVO不同的是,KVO是键值观察,只能观察一个值,这就是区别 转载于:https ...

  9. 点击Notification正确回调到之前已经放置在后台的Task中的对应Activity,而不是创建它的一个新实例...

    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE ...

最新文章

  1. 关于request取中文字符串变?的解决办法
  2. java运行按钮在哪里_[tkinter按钮命令已在程序启动时运行
  3. String案例 获取一个字符串在另一个字符串中出现的次数(两种方法)
  4. 695.岛屿的最大面积(026)BFS
  5. 来来来!一次搞定各种数据库 SQL 执行计划:MySQL、Oracle
  6. 随想录(c编译器的实现)
  7. 用深度学习来解析梦境中出现的物体
  8. win8系统intellij输入中文问题
  9. 转载js实现打印功能
  10. fatal error: libmemcached/memcached.h: No such file or directory
  11. WEB前端基础-HTML CSS总结.xmind思维导图
  12. 鸿蒙系统是怎样一种系统,鸿蒙系统pc版怎么安装 鸿蒙系统pc版安装教程
  13. 【每日最爱一句】2013.07.18
  14. 大学物理实验 基本量的测量
  15. 智慧点餐系统源码 扫码点餐小程序源码
  16. 基层管理者的第一步——从“我”变成“我们”
  17. 数据标注员是职位,人工智能训练师是职业
  18. 自制VOC2007数据集——train、trainval、val、test文件的生成
  19. 深度学习(三)——深层神经网络
  20. [Demo]提取个人博客园闪存+评论

热门文章

  1. 如何在html中选择wrap,jQuery - .wrap() 使用HTML包裹选取的元素
  2. mysql 字节流_字节流至缓冲流
  3. python基础网易_看看你的Python基础怎么样?
  4. spring 全局变量_Spring 十个错误的使用姿势!
  5. 技术选型都做不好,难怪自动化做得这么费力...
  6. php 替换所有中文为空,php 匹配替换中文
  7. 扇贝和不背单词_你还没找到中意的背单词APP?我都试过,我来帮你盘点盘点
  8. Multidimensional Scaling (MDS)
  9. lesson 2.4 - Converting MEL Commands to Python
  10. 可视化类激活的热力图