Should I show ongoing notification when using WorkManager in Oreo and above?

Viewed 590

When executing a task in the doWork() method of a Worker which runs in the background (for example, a worker which runs periodically in the background using PeriodicWorkRequest), should I show an ongoing notification? If so, how do I go about it? Let's say I have something like:

class NoticeWorker(context: Context, params: WorkerParameters)
    : Worker(context, params) {

    override fun doWork(): Result {
        // do some work here
        return Result.SUCCESS
    }

}

which is then executed with

WorkManager.getInstance().enque(workerObj)
1 Answers

No, on Android version >= 23 (which includes Oreo), WorkManager will default to using the JobScheduler to actually enqueue and execute work. From the JobScheduler docs, we see that:

While a job is running, the system holds a wakelock on behalf of your app. For this reason, you do not need to take any action to guarantee that the device stays awake for the duration of the job.

The idea is that using WorkManager allows the system to decide when and how to do things in the background, which is good for performance, so they're trying to make it as easy as possible for us and all of this logic is abstracted away. You only need to specify what needs to be done.

Related