首页 > 代码库 > [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
情况是这样的,使用NotificationManager触发多个Notification:
Java代码
- 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参数):
Java代码
- 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被覆盖?
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。