Android WorkManager setForegroundInfo

Viewed 726

I followed the official guide to set up a long-running worker with WorkManager (Kotlin version). Now I need to update the notification with the operation progress. The documentation says:

// Calls setForegroundInfo() periodically when it needs to update // the ongoing Notification

However I can't find any setForegroundInfo() method. So how do you update the ongoing notification?

4 Answers

It's a typo in the documentation. A Kotlin app should call the CoroutineWorker method setForeground(), a suspending function that makes the CoroutineWorker run in the context of a foreground service. Example:

setForeground(createForegroundInfo(myProgress))

The documentation contains another error: The ForegroundInfo constructor has two arguments, not one. So the code:

return ForegroundInfo(notification)

should be

return ForegroundInfo(notificationId, notification)

where notificationId is an integer that uniquely identifies the notification.

I can't find any setForegroundInfo() method

Same to me, looks like the document is misleading the reader.

So how do you update the ongoing notification?

Simply use the NotificationManagerCompat.notify() method, that's it.

Instead of searching for setForegroundInfo(), you should use setForegroundAsync()

Related