WorkManager or AlarmManager for a daily request-then-notification work?

Viewed 1120

This is the use case : the user set a daily notification with a specific time. At the specified time, a network request is made to fetch some data and then a notification is displayed using the retrieved data. I am not sure whether I should use AlarmManager or WorkManager to implement this use case.

As I understand, AlarmManager is best for scheduling at a precise time. But without network, the job will fail and I prefer the job to be deferred to respect the network constraint than failing at execution. For this type of constrained work, with a guarantee of final execution, WorkManager looks like the best solution, using a OneTimeWorkRequest with an initial delay so that it is executed at the right time.

1 Answers

Comparing AlarmManager and WorkManager, WorkManager wins for some reasons:

1) AlarmManager starting from Kitkat, the alarms may be shifted by OS to reduce the wakeup of the device to reduce battery usage. Check official documentation for more details.

2) Since you aren't going to define a specific timing for the notification, I mean here you're not going to use Calender for a specific time maybe 3:00 PM, use WorkManager because you have PeriodicWorkRequest in WorkManager. Note that you aren't permitted to make PeriodicWorkRequest less than 15 mins. Check PeriodicWorkRequest in official documentation

3) WorkManager now replaces all APIs for background jobs, JobScheduler, Firebase JobDispatcher.

4) WorkManager works perfectly with Coroutines

This is my humble opinion if you have any concerns kindly reply.

Happy coding

Related