菜鸟学android——Notification、PendingIntent问题
来源:互联网 发布:新开户数据 编辑:程序博客网 时间:2024/05/28 11:48
先描述一下我遇到的问题吧:
后台服务有可能会发送3种通知信息,均指向同一个Activity,只是Intent中所带的信息不一样。当3个通知同时出现时,问题就来了,点击通知,打印Intent所带的信息,发现都一样!
具体看下发送通知的代码:
private void sendNotification(int id) {Intent intent = new Intent(BBSService.this, BBSListActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);if (id == BBS_REPLY_ID) {intent.putExtra("reply", true);intent.putExtra("at", false);intent.putExtra("mail", false);} else if (id == BBS_AT_ID) {intent.putExtra("reply", false);intent.putExtra("at", true);intent.putExtra("mail", false);} else if (id == BBS_MAIL_ID) {intent.putExtra("reply", false);intent.putExtra("at", false);intent.putExtra("mail", true);}intent.putExtra("id", id);Notification BBSNotification = new Notification();BBSNotification.icon = R.drawable.ic_launcher;BBSNotification.tickerText = "爱邮";BBSNotification.when = System.currentTimeMillis();BBSNotification.defaults = Notification.DEFAULT_SOUND;BBSNotification.defaults |= Notification.DEFAULT_VIBRATE;BBSNotification.flags |= Notification.FLAG_AUTO_CANCEL;PendingIntent mobilePi = PendingIntent.getActivity(BBSService.this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);if (id == BBS_REPLY_ID) {BBSNotification.setLatestEventInfo(BBSService.this, "论坛消息","新回复我的文章件", mobilePi);} else if (id == BBS_AT_ID) {BBSNotification.setLatestEventInfo(BBSService.this, "论坛消息","新@我的文章", mobilePi);} else if (id == BBS_MAIL_ID) {BBSNotification.setLatestEventInfo(BBSService.this, "论坛消息", "新邮件",mobilePi);}notificationManager.notify(id, BBSNotification);}
通过深入了解,大概明白问题在哪里了。
PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)方法并不能保证每次返回的PendingIntent都是一个新的。如果这个PendingIntent已经存在,那么就会按照flags位的设置对intent进行处理,我这里用的是PendingIntent.FLAG_UPDATE_CURRENT,那么每次都会更新intent,也就是说如果发送了3个通知,那么他们所携带的intent都会是最后一次更新的那个intent。
明白了问题所在,那么解决方法就呼之欲出了,只要保证PengdingIntent的配置不同,那么getActivity返回的必然就是新的PendingIntent。这里把requestCode值定义为id值,就可以了。
0 0
- 菜鸟学android——Notification、PendingIntent问题
- Android PendingIntent Notification
- 菜鸟学android——paint画虚线的问题
- Android开发--Notification和PendingIntent
- android之Notification和PendingIntent
- Android Notification自定义PendingIntent 详解
- Notification(二)——PendingIntent的flag导致数据相同的问题
- 菜鸟Android学习之路27——notification
- Notification PendingIntent
- notification PendingIntent跳转数据不更新问题
- Notification&PendingIntent 发现问题+解决+思考
- Android--PendingIntent 实现发送通知notification
- android notification pendingIntent remoteviews的基础介绍
- Android Notification中PendingIntent.Flag的应用
- Android问题集锦---显示多个notification时PendingIntent的Intent被覆盖
- 菜鸟学android——EditText弹出输入法带来的3个问题
- 菜鸟学android——布局文件的命名
- 菜鸟学Android——横屏竖屏自适应布局
- 搭建hive运行环境
- Cloud Foundry开源项目走向开放管理模式
- Launcher的简单讲解二
- 从待遇和前景上看.net都不如java
- Android 系统设置中显示设置之屏幕旋转和字体设置篇
- 菜鸟学android——Notification、PendingIntent问题
- 资讯_CHM格式帮助文档打开后,链接无效,不能查看信息时,怎么办?
- PHP Excel
- Window Service 创建:在后台执行一个服务,可定时做一些操作,如轮询数据库,定时发邮件
- PDF格式转换器免费下载
- 第9周项目5-程序填充题(2)
- 掉电脑有毒
- 使用Java程序调用本地转换盒作业,资源库中的转换和作业(kettle4.2)
- R正则表达式30分钟入门教程