Unable to cancel periodic work request running foreground

Viewed 268

I have got a periodic work request running foreground. Using the following version of WorkManager

androidx.work:work-runtime-ktx:2.5.0-alpha03 

Here is the work request.

val workRequest = PeriodicWorkRequestBuilder<MyWorker>(15, TimeUnit.MINUTES)
                   setConstraints(constraints).build()

And on the worker class, I set it to foreground.

class MyWorker(context, params) : CoroutineWorker(context, params) {
    override suspend fun doWork(): Result {
         setForeground(createForegroundInfo())

         //rest of the logic
    }

    private void createForegroundInfo() {
       val cancelIntent = WorkManager.getInstance(context).createCancelPendingIntent(id) //work request id
       val notification = NotificationCompat.Builder(context, channelId)
                      .setContentTitle("Test Title")
                     .addAction(actionIconRes, "Cancel", cancelIntent).build()
       return ForegroundInfo(notificationId, notification)
    
    }
}

When I run it, I can see the notification coming up with cancel action. When clicked, the notification disappears momentarily and appears again. Nothing happens on further clicks on cancel action. There is no cancel signalled at all either. What could potentially be a stopper? Any clue?

1 Answers

I think I found the problem. I had nested suspending functions which were causing the Worker to cancel completely. Now that I have changed those functions, it's all working now.

Related