Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Notification intent to clear it self

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

like image 681
John Avatar asked Aug 13 '10 17:08

John


6 Answers

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;
like image 119
Pentium10 Avatar answered Oct 11 '22 11:10

Pentium10


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

Example:

notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
like image 43
Proxy32 Avatar answered Oct 11 '22 12:10

Proxy32


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;
like image 36
sandalone Avatar answered Oct 11 '22 13:10

sandalone


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.

like image 36
iandisme Avatar answered Oct 11 '22 13:10

iandisme


 /** 
      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);
like image 44
swathi Avatar answered Oct 11 '22 12:10

swathi


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.

like image 39
Amal Dev S I Avatar answered Oct 11 '22 12:10

Amal Dev S I