Android display notification with no duplicate notifications

Source: Internet
Author: User

In the development of Android often use notification to show the notice, but before the code written out of an app can only have a notification, there is a new notification when they will overwrite the previous notification, has not known why, good, words do not say, first paste my previous code

    private void Shownotification (String title, context context) {Notificationmanager Notificationmanager = (notif        Icationmanager) context. Getsystemservice (Android.content.Context.NOTIFICATION_SERVICE);        Notification Notification = new Notification (R.drawable.ic_launcher, "XXX", System.currenttimemillis ());        Notification.flags = Notification.flag_auto_cancel;        Notification.defaults |= notification.default_vibrate;        Notification.defaults |= Notification.default_sound;        Notification.defaults |= notification.default_lights;        Notification.vibrate = new long[]{0, 100, 200, 300};        Intent Intent = null;        if (Pushtype = = 1) {intent = new Intent (context, advertisement.class);        } else if (Pushtype = = 2) {intent = new Intent (context, homepage.class);        } else if (Pushtype = = 3) {intent = new Intent (context, orderlist.class); } pendingintent ContentinteNT = pendingintent.getactivity (context, 0, intent, 0);        Notification.setlatesteventinfo (Context, "XXX", title, contentintent);    Notificationmanager.notify (111, notification); }

These days, read the android about notification source code, found a description of the Notify function

/**     * Post a notification to being shown in the status bar. IF a notification     with * the same ID have already been posted by your application and have not yet been canceled, it
   
    * 'll is replaced by the updated information.     *     * @param ID A identifier for this notification unique within your     *        application.     * @param notification A {@link notification} object describing to show the user. Must not     * is        null.     */Public    void notify (int id, Notification Notification)    {        notify (null, ID, Notification);    }
   

  

As can be seen here, the first parameter ID in notification is the ID of each notification, according to common sense to infer, as long as the ID, the nature will overwrite, in this case, then use the timestamp instead of the above notation, the new code is as follows:

  private void Shownotification (String title, context context) {int requestcode = (int) system.currenttimemillis ()        ; Notificationmanager Notificationmanager = (notificationmanager) context. Getsystemservice (android.content.c Ontext.        Notification_service);         Notification Notification = new Notification (R.drawable.ic_launcher, "90 home Wash Car", system.currenttimemillis ());        Notification.flags = Notification.flag_auto_cancel;        Notification.defaults |= notification.default_vibrate;        Notification.defaults |= Notification.default_sound;        Notification.defaults |= notification.default_lights;        Notification.vibrate = new long[]{0, 100, 200, 300};        Intent Intent = null;        if (Pushtype = = 1) {intent = new Intent (context, advertisement.class);        } else if (Pushtype = = 2) {intent = new Intent (context, homepage.class); } else if (Pushtype = = 3) {intent = new Intent (context, Orderlist.class);        } pendingintent contentintent = pendingintent.getactivity (context, 0, intent, 0);        Notification.setlatesteventinfo (Context, "90 home Wash Car", title, contentintent);    Notificationmanager.notify (requestcode, notification); }

  

Here, I put notify () inside the ID parameter into a timestamp, compile, run, as if successful, so that you can display different notifications ~

= = Although successful, but in the source can be seen, there is an overload

 /** * Post a notification to being shown in the status bar. IF a notification with * the same tag and ID have already been posted by your application and have not yet been * CA     Nceled, it'll be replaced by the updated information.  * * @param tag A string identifier for this notification.     May is {@code null}.  * @param ID An identifier to this notification.     The pair (tag, id) must be unique * within your application. * @param notification A {@link notification} object describing what to * show the user.     Must not is null.        */public void notify (String tag, int id, Notification Notification) {int[] idout = new Int[1];        Inotificationmanager service = GetService ();        String pkg = Mcontext.getpackagename ();            if (notification.sound! = null) {Notification.sound = Notification.sound.getCanonicalUri (); if (strictmode.vmfileuriexposureenabled ()) {Notification.sound.checkFileUrIexposed ("Notification.sound");        }} if (LOCALLOGV) log.v (TAG, pkg + ": Notify (" + ID + "," + Notification + ")");        Notification stripped = Notification.clone ();        Builder.stripfordelivery (stripped); try {service.enqueuenotificationwithtag (pkg, mcontext.getoppackagename (), tag, id, stripped            , Idout, Userhandle.myuserid ());            if (id! = idout[0]) {LOG.W (TAG, "Notify:id corrupted:sent" + ID + ", got Back" + idout[0]); }} catch (RemoteException e) {}}

  

This is the actual function of the previous notifiy () method call, but the tag is set to NULL in the previous call, in the comments of this code said tag and ID can be used to distinguish between different notifications, we can use these two mixed together to achieve similar to QQ-like effect

Android display notification with no duplicate notifications

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.