29

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

John
  • 517
  • 2
  • 8
  • 16

6 Answers6

74

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;
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • My code is already using FLAG_AUTO_CANCEL but actually the notification is not going away on click. – John Aug 14 '10 at 09:16
  • 17
    Your code uses wrong (puts in defaults field), you need to bitwise-or in the flags field like this: `notification.flags |= Notification.FLAG_AUTO_CANCEL;` – Pentium10 Aug 14 '10 at 16:11
  • 2
    Thanks notification.flags |= Notification.FLAG_AUTO_CANCEL; Worked. – John Aug 15 '10 at 09:04
  • Flag doesn't seem to be working here. Further setting this flag causes continuously playing notification sound – Murtuza Kabul Sep 17 '12 at 12:25
19

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

Example:

notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
Proxy32
  • 711
  • 5
  • 18
10

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;
The Hungry Androider
  • 2,274
  • 5
  • 27
  • 52
sandalone
  • 41,141
  • 63
  • 222
  • 338
1
 /** 
      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);
Paulo Miguel Almeida
  • 2,114
  • 31
  • 36
swathi
  • 673
  • 1
  • 5
  • 7
  • cancelling the notification right after `notify()` causes the notification is not being displayed at all. – Aryo Feb 08 '14 at 04:57
1

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.

iandisme
  • 6,346
  • 6
  • 44
  • 63
  • If you want by code to cancel the notification use the `NOTIFICATION_ID` on cancel call. – Pentium10 Aug 13 '10 at 18:17
  • Yes i would be calling that from a button, but i would like to call that from the code when the user selects the notification. – John Aug 14 '10 at 09:18
  • Works-for-me solution, however a bit messy (additional service to be written). But in Service I have access to calling Intent in which the notification ID as argument can be given. – Danubian Sailor Feb 20 '12 at 15:17
1

Using setContentIntent should solve your problem:

.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));

For example:

NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("title")
        .setAutoCancel(true)
        .setContentText("content")
        .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());

Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.

Amal Dev S I
  • 938
  • 13
  • 18