[Problem solving] pendingintent intent are overwritten when multiple notification are displayed?

Source: Internet
Author: User

This is the case, using Notificationmanager to trigger multiple notification:

Java code
  1. Private Notification genrenotification (context context, int icon, string tickertext, string title, string content, int Ent Intent) {
  2. Notification Notification = new Notification (icon, Tickertext, System.currenttimemillis ());
  3. Pendingintent pendintent = pendingintent.getactivity (context, 0, intent, pendingintent.flag_update_current);
  4. Notification.setlatesteventinfo (context, title, content, pendintent);
  5. Notification.flags |= Notification.flag_auto_cancel;
  6. return notification;
  7. }
  8. ...
  9. Mnotificationmanager.notify (Id_1,
  10. Genrenotification (Mcontext, Icon_res,
  11. NotifyText1, NotifyTitle1, NotifyText1, intent_1));
  12. ...
  13. Mnotificationmanager.notify (Id_2,
  14. Genrenotification (Mcontext, Icon_res,
  15. NotifyText2, NotifyTitle2, NotifyText2, intent_2));
  16. ...
  17. Mnotificationmanager.notify (Id_3,
  18. Genrenotification (Mcontext, Icon_res,
  19. 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, genrenotific ation (Mcontext, Icon_res, NotifyText2, NotifyTitle2, NotifyText2, intent_2)) ... mnotificationm Anager.notify (Id_3, Genrenotification (Mcontext, Icon_res, NOTIFYTEXT3, nOtifyTitle3, NOTIFYTEXT3, intent_3)); 

The visible ID and intent are different, and the generated pendingintent correspond to different intent. However, you will find that no matter which notification, pass back is the last notify intent. This is intent_3.

Looking for a long time, try to change pendingintent flag also no fruit, and finally in this post to find the answer (Csdn post), I come to summarize:

The problem is mainly in pendingintent.getactivity (), the second parameter of the API document, although the parameter is not used (the given example is also directly written 0), is actually through this parameter to distinguish between different intent, if the ID is the same, It will cover up the previous intent. So always get to the last intent.

As long as each different intent corresponds to pass a separate ID on it, the above function is modified as follows (add ID parameter):

Java code
  1. Private Notification genrenotification (context context, int icon, string tickertext, string title, string content, int ent intent, int id) {
  2. Notification Notification = new Notification (icon, Tickertext, System.currenttimemillis ());
  3. //The problem is here ID.
  4. Pendingintent pendintent = pendingintent.getactivity (context, ID, intent, pendingintent.flag_update_current);
  5. Notification.setlatesteventinfo (context, title, content, pendintent);
  6. Notification.flags |= Notification.flag_auto_cancel;
  7. return notification;
  8. }
  9. ...
  10. Mnotificationmanager.notify (Id_1,
  11. Genrenotification (Mcontext, Icon_res,
  12. NotifyText1, NotifyTitle1, NotifyText1, Intent_1, id_1));
  13. ...
  14. Mnotificationmanager.notify (Id_2,
  15. Genrenotification (Mcontext, Icon_res,
  16. NotifyText2, NotifyTitle2, NotifyText2, Intent_2, id_2));
  17. ...
  18. Mnotificationmanager.notify (Id_3,
  19. Genrenotification (Mcontext, Icon_res,
  20. NOTIFYTEXT3, NotifyTitle3, NotifyText3, Intent_3, id_3));

[Problem solving] pendingintent intent are overwritten when multiple notification are displayed?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.