java - Dismiss current notification on Action clicked -


i have custom notification action button:

public class notificationreceiver extends com.parse.parsepushbroadcastreceiver {     @override     public void onpushreceive(context context, intent intent) {      ...      notificationactivity notification = new notificationactivity();     notification.createnotification(context, message, notificationid);      log.i("tag", "notification received!"); } 

public class notificationactivity {      public int notificationid;      public void createnotification(context context, string message, string studentid, string notificationid){          this.notificationid = integer.parseint(notificationid);           // manager          notificationmanager notificationmanager =           (notificationmanager) context.getsystemservice(context.notification_service);           // notification          notification.builder mbuilder = new notification.builder(context);          mbuilder.setcontenttitle("my title");          mbuilder.setcontenttext(message);          mbuilder.setdefaults(notification.default_vibrate);          mbuilder.setautocancel(true);          mbuilder.setstyle(new notification.bigtextstyle()              .bigtext(message));           // cancel intent          intent cancelintent = new intent(context, cancelnotification.class);          bundle extras = new bundle();          extras.putint("notification_id", this.notificationid);          cancelintent.putextras(extras);          pendingintent pendingcancelintent =               pendingintent.getbroadcast(context, 0, cancelintent, pendingintent.flag_update_current) ;          mbuilder.addaction(r.drawable.notification_close, "fechar", pendingcancelintent);          // notify         notification notification = mbuilder.build();         notificationmanager.notify(integer.parseint(notificationid), notification);     }      public static class cancelnotification extends broadcastreceiver {          private int id;          @override         public void onreceive(context context, intent intent) {              id = intent.getintextra("notification_id", 1);              notificationmanager notificationmanager =                   (notificationmanager) context.getsystemservice(context.notification_service);              notificationmanager.cancel(id);         }     } } 

i want cancel notification clicked action button "close".

i know need id of notification cancel it, way did code, when click "close" button , create class cancelnotification extends broadcastreceiver i'm getting notification id of last notification, , so, closing last notification if click on first notification created.

what doing wrong?

i found it

you pendingintent sending request code == 0;

since have multiple notifications, each 1 should use different requestcode.

so, try change:

from:

pendingintent pendingcancelintent =           pendingintent.getbroadcast(context, 0, cancelintent, pendingintent.flag_update_current) ; 

to:

pendingintent pendingcancelintent =           pendingintent.getbroadcast(context, this.notificationid, cancelintent, pendingintent.flag_update_current) ; 

i tested code here , it's working after change did.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -