Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to schedule notifications using WorkManager?

I want to schedule a notification everytime the user add a note in the database for a specific time. While there are multiple ways to do it using AlarmManager, BroadcastReceiver etc. How can it be done using WorkManager?

like image 969
Ankur Gupta Avatar asked May 11 '18 20:05

Ankur Gupta


People also ask

How do I schedule notifications on Android?

On Android, you will need to open Settings. Then choose Sound>Do Not Disturb>Schedules. On this screen, you have several options to set the days and times for Do Not Disturb, choose which apps to apply it to, and set exceptions.

What is the use of WorkManager in Android?

Android WorkManager is a background processing library which is used to execute background tasks which should run in a guaranteed way but not necessarily immediately. With WorkManager we can enqueue our background processing even when the app is not running and the device is rebooted for some reason.

What is the difference between WorkManager and JobScheduler in Android?

You should also note that WorkManager is part of androidx while JobScheduler is part of old support libraries.


2 Answers

WorkManager isn't appropriate for work that needs to happen at a particular time.

You should use AlarmManager, and specifically AlarmManagerCompat.setExactAndAllowWhileIdle(), to get a callback at a specific time.

like image 81
ianhanniballake Avatar answered Sep 20 '22 13:09

ianhanniballake


You can show notification with the help of workmanager. Workmanger can be used for scheduling single occurrence task or periodic occurrence task as well as we can schedule task at a particular time.

OneTimeWorkRequest request= new OneTimeWorkRequest.Builder(CustomWorkerClass.class)
.setInitialDelay(delayedTime, TimeUnit.MILLISECONDS)
.addTag("TAG")
.build();

'delayedTime' -> calculate time when to trigger notification

for more implementation details click here. To read more regarding workmanager click here

like image 33
Sadashiv Avatar answered Sep 16 '22 13:09

Sadashiv