Search code examples
androidandroid-alarmsandroid-workmanager

Using Android WorkManager for local Notification instead of AlarmManager


User selects a time to notify a message as a reminder that could repeat for everyday or only on certain day of the week.Previously I did this task by setting time to AlarmManager

        Calendar calSet= Calendar.getInstance();
        //calSet.set(Calendar.DAY_OF_WEEK, dayOfWeek); for a particular day of the week
        calSet.set(Calendar.HOUR_OF_DAY, hrs));
        calSet.set(Calendar.MINUTE, min));
        calSet.set(Calendar.SECOND, 00);
        calSet.set(Calendar.MILLISECOND, 00); 
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

Now AlarmManager stops working when the device is at Doze .I cannot use setAndAllowWhileIdle because I need to repeat the notification message.

Now I have one option to use WorkerManager,but I have no idea of how to set the time as done in AlarmManager

      PeriodicWorkRequest.Builder(SetJobWork.class,1,TimeUnit.DAYS)
            .addTag(Id).setConstraints(constraints).build()

Suppose I need to run the task at 1.00 pm daily,what is the way out.


Solution

  • Now I have one option to use WorkerManager,but I have no idea of how to set the time as done in AlarmManager

    WorkManager does not support your scenario, sorry.

    I cannot use setAndAllowWhileIdle because I need to repeat the notification message.

    You can manually repeat by calling setAndAllowWhileIdle() to schedule the next alarm as part of processing the PendingIntent for your last alarm.