Dismiss current notification on Action clicked

I found it

You pendingIntent is always sending request code == 0;

Since you have multiple Notifications, each one should use a different requestCode.

So, try to 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 your code here and it's working after the change I did.


It is always better to use a Notification builder. Heres an example:

    NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(this);
    mBuilder.setContentTitle("Your title");
    mBuilder.setOnlyAlertOnce(true);
    mBuilder.setAutoCancel(true);
    mBuilder.setContentText("main content")
    mBuilder.setSubText("subtext")

Next you will have create an intent to which activity you want to open on notification clicked

    intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

Then create your notification manager

    notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = mBuilder.build();
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(notificationID, notification);

notificationID can be any integer value. Using this type gives you the advantage of always following android norms for notifications.