I want to display a notification while a Worker is running in the background. I can do that with something like the code below:
override suspend fun doWork(): Result {
val manager = NotificationManagerCompat.from(applicationContext)
val channel = "some_channel"
val id = 15
val builder = NotificationCompat.Builder(applicationContext, channel)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
manager.createNotificationChannel(NotificationChannel(channel, "DefaultName", NotificationManager.IMPORTANCE_LOW))
builder
.setOnlyAlertOnce(true)
.setOngoing(true)
.setAutoCancel(false)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setTicker("Background Task")
.setContentText("Starting")
.setProgress(0, 0, true)
setForeground(ForegroundInfo(id, builder.build()))
delay(500)
manager.notify(id, builder.setContentText("Progress A").setProgress(20, 0, false).build())
for (i in 1..20) {
delay(100)
manager.notify(id, builder.setProgress(20, i, false).build())
}
delay(500)
manager.notify(id, builder.setContentText("Progress B").setProgress(2, 0, false).build())
delay(1000)
manager.notify(id, builder.setProgress(2, 1, false).build())
delay(1000)
manager.notify(id, builder.setProgress(2, 2, false).build())
delay(1000)
manager.notify(id, builder.setContentText("Done!").setProgress(0, 0, false).build())
delay(500)
return Result.success()
}
But I also want to display the result of the worker in the notification and keep it until the user sees and clears it, yet the notification is always cleared at the end of the work.
How can I keep the notification alive? .setOngoing(true)
and .setAutoCancel(false)
didn't help.