136

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);
Flo
  • 26,717
  • 14
  • 82
  • 124

7 Answers7

312

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

Kamil Lelonek
  • 13,631
  • 10
  • 60
  • 87
  • 2
    So, what differences create notification using Notification `mNotificationManager.notify(1,notification);` and using NotificationBuilder `mNotificationManager.notify(1, mBuilder.build());`? Thanks. – Yohanes AI Jul 10 '14 at 11:54
  • 9
    This answer should be accepted, it's more in-line with current android design doctrine – jmaculate Aug 27 '14 at 16:01
  • This answer is correct. Accepted one works but not always. There is problem when there are stacked notifications on GCM (or whatever you are using). Once you ping notification server it returns with a lot notifications and sometimes it just loops the notification appearance. – Nikola Milutinovic Oct 01 '14 at 01:01
  • 6
    `notificationBuilder.setAutoCancel(true);` is not working for me. Should I put before my Pending Intent? – Kairi San Feb 15 '16 at 03:36
129
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

Roman Holzner
  • 5,098
  • 2
  • 19
  • 30
synic
  • 25,374
  • 17
  • 102
  • 140
  • 3
    This is not the correct answer. `Notification.DEFAULT_LIGHTS` is part of the `Notification.defaults` class, not the `Notification.flags` class. See my answer for the appropriate setters. – Darcy Mar 02 '12 at 15:13
  • notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL; this method is working thank you synic. – Ravikumar11 Apr 26 '13 at 04:39
  • 1
    The code in this answer resulted in the notification sound being played multiple times. Check out the other answers. – ban-geoengineering Jan 16 '15 at 16:45
27
// 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;
Darcy
  • 4,780
  • 12
  • 51
  • 76
  • 1
    I have gone through the android docs. I don't quite get when flags should be used. Why isn't just notification.defaults=notification.DEFAULT_LIGHTS enough to show the lights. Because the vibrate and sound work without the flag. – Ashwin Aug 17 '12 at 17:21
  • Am using the NotificationBuilder, NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(android.R.drawable.ic_popup_sync) .setContentTitle("New Tweet") .setContentText("There are " + count + " tweets"); mBuilder.setDefaults(NotificationCompat.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL); – Joseph Oct 16 '14 at 20:03
13

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

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

artdias90
  • 725
  • 1
  • 9
  • 16
2

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

Machine Tribe
  • 771
  • 7
  • 13
1

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);
user1810087
  • 4,541
  • 1
  • 34
  • 66
Sachin Suthar
  • 650
  • 9
  • 22
1

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

Angel
  • 168
  • 1
  • 9