'Notification is not cancelled after calling notification.cancel

I create notification with action button. When clicking on action button, broadcast receiver is called. I am passing the notification ID in the intent

In the broadcast receiver I do the following

int notifId = intent.getIntExtra(Constants.NOTIF_ID, 0);
NotificationManager mNotificationManager = 
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);   

mNotificationManager.cancel(notifId);

This is how I generate the notification

int notifId = Util.random.nextInt(9000);
Intent mIntent = new Intent(con, NotificationBroadcastReceiver.class);
mIntent.putExtra(Constants.NOTIF_CODE, codeReason);
mIntent.putExtra(Constants.NOTIF_ID, notifId);

PendingIntent mPendingIntent = PendingIntent.getBroadcast(con, 0, mIntent , 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(con)
        .setSmallIcon(R.drawable.icon)
        .setContentTitle("test")
        .setPriority(Notification.PRIORITY_MAX)
        .setAutoCancel(true)
        .addAction(R.drawable.ic_launcher, "action", mPendingIntent);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(con, MainActivity.class);

PendingIntent resultPendingIntent = PendingIntent.getActivity(
        con, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT
);

mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
        (NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);

mBuilder.setDefaults(
        Notification.DEFAULT_SOUND |
        Notification.DEFAULT_VIBRATE |
        Notification.DEFAULT_LIGHTS
);

// mId allows you to update the notification later on.
mNotificationManager.notify(notifId, mBuilder.build());

However the notification does not get hidden/dismissed although I know I am hitting the code ( using log statements).

Why is that?



Solution 1:[1]

  1. Check your id if it's zero.
  2. Check the notification is not binded with other Service by startForeground().

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Roc Boronat