[问题解决]同时显示多个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));
可见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));
:D ,多谢。 这个帖子看过,应该是用透明图标一类实现的效果。 实现了一下,不是特别理想。
也测试了一下主流软件的Notification效果,例如:firefox,dorpBox,evernote等 没有像Gmail那么实现的,估计是Google自己做的特效吧。
对于隐藏Notification的ICON,最近看到了这样一篇文章,也许有帮助...回头来试试,发觉Notification还有好多参数没有仔细研究过. http://stackoverflow.com/questions/2855110/android-no-icon-for-notification
:D :D
我们项目中有这么一个要求。
标题栏上只显示一个计数的ICON 通知领域内要显示每次发送的Notification,并且Notification对应的PendingIntent内容也不相同。
据说客户是看了Gmail的效果提出来的。
真是头疼啊,只能看看有没有方法解析Gmail的源代码了。
就是通过显示图标来提示用户的吧,没有图标怎么提示呢。应该不能隐藏。
说的多个图标,如果你是同一个提示内容是可以使用PendingIntent.FLAG_UPDATE_CURRENT等属性来控制的。但如果是要提示并绑定不同的Intent,目前确实是一条Notification对应一个独立的ICON的,暂不知有没其他解决方法,mark下研究下。 :)
太强悍了,老大,我也是这个问题,搞了好久,差文档也么搞定啊。
多谢你这篇文章了。 :D :D :D :D
再问一个问题。
发送Notification的时候,怎么隐藏状态栏上的图标啊。
如果发送多个Nofication,显示多个图标,画面非常不友好。