yoshi24
yoshi24

Reputation: 3177

How to get rid of ongoing notification when a service is finished?

I am using this to notify the user of an ongoing service in the background.

 nm = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
              CharSequence from = "App";
              CharSequence message = "Getting Latest Items...";
              PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
               notif = new Notification(R.drawable.icon,
                        "Getting Latest Items...", System.currentTimeMillis());
                      notif.setLatestEventInfo(this, from, message, contentIntent);
                      notif.flags = Notification.FLAG_ONGOING_EVENT;
                      nm.notify(1, notif);

Now when the service is finished, how do i get this to dissapear?

Upvotes: 0

Views: 538

Answers (2)

PandaPaul
PandaPaul

Reputation: 156

When your service stops, its onDestroy() should get called. You could put the cancel() that Viren mentioned there. That should wipe your notification when the service is finished doin its thing.

Upvotes: 1

Viren
Viren

Reputation: 2171

Call cancel() on NotificationManager

There are different cancel methods :

public void cancel (int id)
Since: API Level 1

Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.
public void cancel (String tag, int id)
Since: API Level 5

Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.
public void cancelAll ()
Since: API Level 1

Cancel all previously shown notifications. See cancel(int) for the detailed behavior.

Upvotes: 1

Related Questions