Android Notification intent to clear it self

37,714

Solution 1

Check out FLAG_AUTO_CANCEL

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.

EDIT :

notification.flags |= Notification.FLAG_AUTO_CANCEL;

Solution 2

Set the flags in notification.flags instead of notification.defaults.

Example:

notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;

Solution 3

If you're using NotificationCompat.Builder (a part of android.support.v4) then simply call its object's method setAutoCancel

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);

Some guys were reporting that setAutoCancel() did not work for them, so you may try this way as well

builder.build().flags |= Notification.FLAG_AUTO_CANCEL;

Solution 4

The only way I can see of doing this is to have your Notification's Intent point to a background Service. When this Service is launched, it would clear the given Notification using NotificationManager.cancel(int id). The Service would then stop itself. It's not pretty, and would not be easy to implement, but I can't find any other way of doing it.

Solution 5

 /** 
      Post a notification to be shown in the status bar. 
      Obs.: You must save this values somewhere or even pass it as an extra through Intent to use it later
 */
 notificationManager.notify(NOTIFICATION_ID, notification);

 /** 
      Cancel a previously shown notification given the notification id you've saved before
 */
 notificationmanager.cancel(NOTIFICATION_ID);
Share:
37,714
John
Author by

John

Updated on February 17, 2020

Comments

  • John
    John over 4 years

    I have read many examples of how to create notification messages. What i wanted to achieve, is because the notification will be executed by a widget, i would like the notification intent when clicked to clear it self when the user clicks on it.I do not have an activity to return to. The notification for my purposes will just plainly notify, nothing else. So what would be the code of an intent that just clear/cancel itself. The code below is an activity launched by a button(button code not included) the notification will be fired up by a background service.

    CharSequence title = "Hello";
    CharSequence message = "Hello, Android!";
    final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());
    
    notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
    Intent notificationIntent = new Intent(this, AndroidNotifications.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
    
    notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
    notificationManager.notify(NOTIFICATION_ID, notification);
    

    Thanks