Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android notification doesn't disappear after clicking the notification

People also ask

How do I get rid of a notification that won't go away Android?

First, press-and-hold on the persistent notification you want to remove. Another option is to swipe the notification left or right, and then tap on the cogwheel icon shown next to it. Next, tap on the switch next to Permanent to disable it, and then press Save.

How do I remove notifications after click?

You can directly add . setAutoCancel(true) this line into your code in order to remove notification on click.


While building Notification by NotificationBuilder you can use notificationBuilder.setAutoCancel(true);.


notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

From the documentation:

Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user


// Uses the default lighting scheme
notification.defaults |= Notification.DEFAULT_LIGHTS;

// Will show lights and make the notification disappear when the presses it
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;

2016 state: you can use mBuilder.setAutoCancel(true).

Source: https://developer.android.com/reference/android/app/Notification.Builder.html


The answer for me was to use mBuilder.setOngoing(false)


Use the flag Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);

// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

and to launch the app:

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

Intent intent = new Intent(context, App.class);

PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Remove a notification

Notifications remain visible until one of the following happens:

  1. The user dismisses the notification.
  2. The user clicks the notification, and you called setAutoCancel() when you created the notification.
  3. You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
  4. You call cancelAll(), which removes all of the notifications you previously issued.
  5. If you set a timeout when creating a notification using setTimeoutAfter(), the system cancels the notification after the specified duration elapses. If required, you can cancel a notification before the specified timeout duration elapses.

For more details see: https://developer.android.com/training/notify-user/build-notification?hl=en