I'm attempting to migrate some network calls to WorkManager CoroutineWorkers, to take advantage of the automatic retries on failure and scheduling powers.
This is the worker I wrote:
@HiltWorker
class RefreshSubscriptionDetailsWorker
@AssistedInject constructor(
@Assisted appContext: Context,
@Assisted workerParams: WorkerParameters
) :
CoroutineWorker(appContext, workerParams) {
override suspend fun doWork(): Result {
return Result.success()
}
}
The code in BaseApplication which configures the request:
val refreshSubscriptionDetailsRequest =
OneTimeWorkRequestBuilder<RefreshSubscriptionDetailsWorker>()
.build()
WorkManager
.getInstance(applicationContext)
.enqueue(refreshSubscriptionDetailsRequest)
I've followed the instructions in the documentation to ensure Hilt is injected correctly:
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider>
@Inject lateinit var workerFactory: HiltWorkerFactory
override fun getWorkManagerConfiguration() =
Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()
The problem I'm having is that when it's queued, it crashes with the below exception:
Fatal Exception: java.lang.IllegalStateException: Not implemented
at androidx.work.CoroutineWorker.getForegroundInfo$suspendImpl(CoroutineWorker.kt:100)
at androidx.work.CoroutineWorker.getForegroundInfo()
at androidx.work.CoroutineWorker$getForegroundInfoAsync$1.invokeSuspend(CoroutineWorker.kt:134)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
I would expect that I would need to implement the getForegroundInfo function if I was going to be using an Expedited request, but I shouldn't need to in this instance, as I'm not asking for an expedited request, or so I've been led to believe by the absense of getForegroundInfo implementations in tutorials, and the patch notes which indicate that you need a ForegroundService for Expedited requests: Patch notes
I'm using WorkManager 2.7.1, Hilt 2.41, trying to run it on Android versions 8 through 12.
You need to override suspend fun getForegroundInfo() in your CoroutineWorker class and return public ForegroundInfo(int notificationId, @NonNull Notification notification) there. It helped me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With