In Android, how do I know the current notification id to clear the notification?


Now in android i put this code in one activity to show notification when a button pressed.

static int notificationCount = 0;


            new View.OnClickListener() {
                public void onClick(View v) {

                    Intent notificationIntent = new Intent(AlertsActivity.this,NotificationActivitty.class);
                    PendingIntent pIntent = PendingIntent.getActivity(AlertsActivity.this,notificationCount,notificationIntent,Intent.FLAG_ACTIVITY_NEW_TASK);

                    // Construct the notification
                    Notification.Builder nBuilder = new Notification.Builder(AlertsActivity.this);
                    nBuilder.setContentTitle("You Have a notification!");
                    nBuilder.setContentText("See Your Notification");
                   nBuilder.addAction(android.R.drawable.stat_notify_call_mute, "go to", pIntent); // from icecream sandwatch - required api 16

                    // Build the notification
                    Notification noti =; // required api 16

                    //Send it to manager
                        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

From Notification manager, Any notification that i clicked on it, it will redirect me to another activity (NotificationActivity)

Now i put this code to clear the notification but it only clear the notification with id 0 so how can i clear the current pressed notification

public class NotificationActivitty  extends Activity{

protected void onCreate(Bundle savedInstanceState) {

    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    // manager.cancelAll(); // Cancel all notifications for this app. from manager


I need to clear the notification by it's id if it's possible.

You should add a Tag to your notification and then clear you notification by providing correct id and correct tag.

You don't need notification counter if you pass the same id, because when notification sees the same id, it clears old notification and puts a new one, unless you want to show that user received multiple notifications.

private static final String TAG = "YourNotification";
private static final int NOTIFICATION_ID = 101;
private Notification notification;
public NotificationManager notificationManager;

//you can create notification with it's own id and tag, text, etc by passing
//these variables into the method (int id, String tag, ... etc)

public void createNotification()
    notification = new Notification.Builder(context)
                       .setContentTitle("Content title")
                       .setContentText("Content text")
                       .addAction(R.drawable.icon, pendingIntentAction)
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(TAG, NOTIFICATION_ID, notification);

to cancel notification simply use this method:

//clears your notification in 100% cases

public void cancelNotification(int id, String tag)
  //you can get notificationManager like this:
  //notificationManage r= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(tag, id);