I am trying to run a simple work every 10 seconds using WorkManager. It works perfectly when app is running on background or foreground. When I close the app (kill the app), the work will not be called anymore.
I call the below code when MainActivity created
fun scheduleNotification(context: Context) {
val workRequest = OneTimeWorkRequest.Builder(NotificationWorker::class.java).setInitialDelay(10000, TimeUnit.MILLISECONDS)
WorkManager.getInstance().enqueueUniqueWork("NotificationWorker", ExistingWorkPolicy.REPLACE, workRequest.build())
Log.d("NotificationWorker", "start")
}
The Worker class
class NotificationWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
Log.d("NotificationWorker", "Working")
NotificationUtil.scheduleNotification(applicationContext) // n times
return Result.success()
}
}
These code work well when my app does not closed.
How can I make it runs even when the app was closed?