1

I'm developing an Android application that needs to remind the user to perform certain tasks, periodically updated. As long as the app is open there are no problems, as I can notify the tasks directly by editing the GUI and activating a sound signal; the problems arise when the app is closed: obviously I cannot launch the app directly because of Android policies (versions 9-13), so the idea would be to show a notification at the exact moment when the task needs to be done (like a Whatsapp message notification). Can I take advantage of the AlarmManager I already use or should I use something else?

Thanks for your attention

Lubron
  • 35
  • 2
  • 15

1 Answers1

0

I think the best way is by using Work Manager. Here is the explanation from the alarm docs.

It's highly recommended that you create an inexact alarm whenever possible. To perform longer work, schedule it using WorkManager or JobScheduler from your alarm's BroadcastReceiver. To perform work while the device is in Doze, create an inexact alarm using setAndAllowWhileIdle(), and start a job from the alarm.

I have the same use case and personally use periodical worker to trigger my alarm for any particular time and find it works. I have never missed any notification at my schedule time as stated in the docs.

WorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts

  • Thanks! It works perfectly also after a device reboot! Do you think that is possible to launch a service (startService/startForegroundService) from one of this Work? I need also to run a service at every midnight – Lubron Jun 26 '23 at 08:20
  • `startService`/`startForegroundService` is no longer recommended for doing background service. The `startForegroundService` has no guarantee that it will be executed after 5 seconds. If the phone memory is unable to handle the service, your app will go to ANR. In my opinion, if you have a backend, it is better to trigger the service through FCM. Otherwise, you can deploy periodic (every 15 minutes) work request to trigger an alarm set at midnight to make sure that your alarm are created once the system kills it. – Annas Surdyanto Jun 27 '23 at 15:08