zoukankan      html  css  js  c++  java
  • 同时显示多个Notification时PendingIntent

    使用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也无果,我来总结下:

    问题主要出在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)); 

     

    互联网 信息聚合网站 : www.yidin.net 聚合互联网最优质资源、协助设计、分享经验、提升自我 欢迎访问(投稿,招聘信息请入)

    欢迎各位同学加入 android 技术二群 222392467 

  • 相关阅读:
    GTK+ 3.6.2 发布,小的 bug 修复版本
    RunJS 新增 Echo Ajax 测试功能
    Mozilla 发布 Popcorn Maker,在线创作视频
    Sina微博OAuth2框架解密
    Mina状态机State Machine
    Mozilla 发布 Shumway —— 纯JS的SWF解析器
    Code Browser 4.5 发布,代码浏览器
    ROSA 2012 "Enterprise Linux Server" 发布
    ltrace 0.7.0 发布,程序调试工具
    Artifactory 2.6.5 发布,Maven 扩展工具
  • 原文地址:https://www.cnblogs.com/ondream/p/Notification.html
Copyright © 2011-2022 走看看