androidservicenotificationsbroadcastreceiverandroid-recents

Show notifications at specific time in the future with app closed and not in Recent on Android


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


Solution

  • 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