131

I want that the notification will be closed after the user is clicking on it. I saw that everybody saying to use flags, but I can't find the flags anywhere because I'm using NotificationCompat.Builder class and not Notification class. Someone have any idea how to make the notification remove by her self?
Here is my code when I'm setting the notification:

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("New Question")
            .setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + "");

    Intent openHomePageActivity = new Intent("com.example.ihelp.HOMEPAGEACTIVITY");
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(openHomePageActivity);

    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);       

    mNotificationManager.notify(0, mBuilder.build());
Yossi Zloof
  • 1,609
  • 3
  • 13
  • 13

5 Answers5

346

Easy, simply call this:

mBuilder.setAutoCancel(true);

Also, while it's not really necessary, if you really want to use FLAG_AUTO_CANCEL, just call this before you call mNotificationManager.notify:

mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
Nabeel K
  • 5,938
  • 11
  • 38
  • 68
ingh.am
  • 25,981
  • 43
  • 130
  • 177
19

Try this....

NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

 ..........
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.push_notify_icon)
            .setContentTitle("New Question!")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true).setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + "");
mBuilder.setContentIntent(contentIntent);

    ..............        


mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, mBuilder.build());
Silambarasan Poonguti
  • 9,386
  • 4
  • 45
  • 38
14

Here is notification:

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_calendar)
            .setContentTitle("My Firebase Push notification")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);

the key behind cancellation on click is:

            .setAutoCancel(true)

i hope it resolves the matter.

Ali Nawaz
  • 2,016
  • 20
  • 30
  • I am displaying the notification on foreground to and i want to dismiss that notification when user tap `.setAutoCancel(true)` already use this , but not working for me for foreground notification. – Arbaz.in Apr 21 '22 at 12:00
3

You can add a flag to your notification:

http://developer.android.com/reference/android/app/Notification.html#FLAG_AUTO_CANCEL

This will dismiss it upon clicking.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
2

In kotlin, you can use:

mBuilder.build().flags.and(Notification.FLAG_AUTO_CANCEL)
Ricardo
  • 2,086
  • 25
  • 35