Android notification doesn't disappear after clicking the notification

AndroidNotifications

Android Problem Overview


If got some issues with a notification I want to show in the notification bar. Although I set the notification flag to Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL the notification doesn't disappear after clicking it. Any ideas what I'm doing wrong?

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

int icon = R.drawable.icon;
CharSequence tickerText = "Ticker Text";
long time = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, time);
notification.flags = Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL; 

Context context = getApplicationContext();
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(this, SilentFlipConfiguration.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,notification);

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

// 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;

Solution 4 - Android

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

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

Solution 5 - Android

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

Solution 6 - Android

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);

Solution 7 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFloView Question on Stackoverflow
Solution 1 - AndroidKamil LelonekView Answer on Stackoverflow
Solution 2 - AndroidsynicView Answer on Stackoverflow
Solution 3 - AndroidDarcyView Answer on Stackoverflow
Solution 4 - Androidartdias90View Answer on Stackoverflow
Solution 5 - AndroidMachine TribeView Answer on Stackoverflow
Solution 6 - AndroidSachin SutharView Answer on Stackoverflow
Solution 7 - AndroidAngelView Answer on Stackoverflow