How can I modify a Notification that is created with StartForeground?

2.4k views Asked by At

I have a Service that needs to be run as a foreground Service. For that to work, a Notification has to be linked to it.

When the service is started, I set the OnGoing to true, and when the user interacts with the Notification to stop the service, I would like to change the current Notification to OnGoing(false). (So the user can dismiss the Notification, if so desired) I want the Notification to still be visible so the user can start the Service again from within the Notification.

Normally, I would have had to use the NoficiationManager to .Notify with a specific ID and later modified it. However, with the call to StartForeground(flags, notification), there is not option to set the ID.

How can I modify an OnGoing(true) Notification (started with the StartForeground call) to OnGoing(false)?

1

There are 1 answers

1
GVillani82 On BEST ANSWER

I think your idea is correct: you have to use an ID and then update the notification using that ID.

The first field of the startForeground method is not a flags but an ID. Here the signature:

void startForeground (int id, Notification notification)

Documentation here

So you can update the notification, using the same ID.

mNotificationManager.notify(sNotificationId, notification);