android - Receive signal after notification clicked -


i fire notification mainactivity class. when user click notification, i'd return mainactivity class , execute method. i'd know which notification clicked (assuming fire multiple notifications different id). here did , didn't work

inside mainactivity.class:

private void shownotification(string title, string message, int id) {     notificationcompat.builder mbuilder =             new notificationcompat.builder(this)             .setsmallicon(r.drawable.ic_launcher)             .setcontenttitle(title)             .setcontenttext(message);      intent resultintent = new intent(this, mainactivity.class);     resultintent.setaction("maction");     pendingintent resultpendingintent = pendingintent.getbroadcast(this, 0, resultintent, pendingintent.flag_update_current);      mbuilder.setcontentintent(resultpendingintent);     notificationmanager mnotificationmanager =         (notificationmanager) getsystemservice(context.notification_service);     mnotificationmanager.notify(id, mbuilder.build()); } 

same inside mainactivity.class create broadcastreceiver class it never got called:

public class mybroadcastreceiver extends broadcastreceiver {     @override     public void onreceive(context context, intent intent) {         string action = intent.getaction();          if(action.equals("maction")) {                 //execute method here         }     }          } 

i did add mybroadcastreceiver.class receiver in androidmanifest.xml:

<receiver android:name=".mybroadcastreceiver" > </receiver> 

as @varun suggestion, here how solve problem.

in shownotification replace .setaction .putextra , change .getbroadcast .getactivity:

    intent resultintent = new intent(this, mainactivity.class);                resultintent.putextra("maction", id); // put id here know notification clicked     pendingintent resultpendingintent = pendingintent.getactivity(this, 0, resultintent, pendingintent.flag_update_current); // change getbroadcast getactivity 

no longer need mybroadcastreceiver class instead add lines in oncreate() intent result:

    if(getintent().hasextra("maction")){         bundle = getintent().getextras();         int id = extra.getint("maction");         if(id == 1) {             //do stuff         }     }