2 years ago
#30502

Vivek Modi
what is difference between setForeground and setForegroundAsync in kotlin
Hey I am working in WorkManager. My code is running in android 11 and below . I want to support WorkManager for sdk 31(android 12). I am reading doc and it confused for my scenario Backwards compatibility and foreground services. I am adding code please have a look and guide me is my code doing right?
implementation "androidx.work:work-runtime-ktx:2.7.1"
NotificationWorker.kt
class NotificationWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams),
KoinComponent {
private val notifications: Notifications by inject()
override fun doWork(): Result {
notifications.createNotification(NotificationType.REMINDER, null)
TrackerHelper.setupNotificationWorker()
return Result.success()
}
}
TrackerHelper.kt
object TrackerHelper {
fun setupNotificationWorker() {
val delayDuration = getDelayMillis()
val dailyWorkRequest =
OneTimeWorkRequestBuilder<NotificationWorker>()
.setInitialDelay(delayDuration, TimeUnit.MILLISECONDS)
.addTag(REMINDER_WORK_TAG)
.build()
WorkManager.getInstance(Application.appContext).enqueue(dailyWorkRequest)
}
private fun getDelayMillis(): Long {
val currentDate = Calendar.getInstance()
val dueDate = getNotificationDueTime()
if (dueDate.before(currentDate)) {
dueDate.add(Calendar.HOUR_OF_DAY, 24)
}
return dueDate.timeInMillis - currentDate.timeInMillis
}
}
The above is perfectly working in android 11 and below version. But It's not working in android 12. So I am reading in official doc Kotlin inside doWork()
they are using setForeground()
and in Java using setForegroundAsync()
. So which is useful in my case. CoroutineWorker and what the use case of getForegroundInfo()
this function and how to proper handle this not to throw error
I am totally confused what function need to be used in my case and please explain me which one is use when with example. Thanks in advance
I am getting this error in android 12
2022-01-10 12:00:00.333 9958-10168/com.example.app.dev E/WM-WorkerWrapper: Work [ id=827b6cde-a3fe-42ab-89f3-92c88af95fd9, tags={ com.example.app.tracker.TrackerNotificationWorker, tracker_reminder_work } ] failed because it threw an exception/error
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: com.example.app.dev: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:516)
at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:311)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
Caused by: java.lang.IllegalArgumentException: com.example.app.dev: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458)
at android.app.PendingIntent.getActivity(PendingIntent.java:444)
at android.app.PendingIntent.getActivity(PendingIntent.java:408)
at com.example.app.notifications.NotificationsNative.launchNotification(NotificationsNative.kt:67)
at com.example.app.notifications.NotificationsNative.createTrackerReminderNotification(NotificationsNative.kt:35)
at com.example.app.notifications.NotificationsNative.createNotification(NotificationsNative.kt:28)
at com.example.app.tracker.TrackerNotificationWorker.doWork(TrackerNotificationWorker.kt:19)
at androidx.work.Worker$1.run(Worker.java:86)
... 3 more
android
kotlin
android-service
android-notifications
android-workmanager
0 Answers
Your Answer