Android - run job in Firebase job scheduler at specific date and time - android

I am using AlarmManager in our app to set alarms that are set at specific date and time. Now recently few users of our app are complaining that these alarms are not popping up. Also, in Android O guidelines, it is mentioned that app should not run any background service and instead should switch to Firebase JobDispatcher. I have following 2 questions
In our app, we do not do any background task except to show notification to user at the specified time and date. Even in this case, should we switch to Firebase Jobdispatcher?
In case we do need to switch to JobDispatcher, how can the Job be set to run at exactly specific date and time?

Because you're not doing any background tasks like network requests or long running CPU operations, I think you should continue using Alarm Manager.
Now recently few users of our app are complaining that these alarms are not popping up.
This is because when doze mode triggers, it is not guaranteed that your alarms will be triggered(see this).
What you can do is use setAndAllowWhileIdle() or setExactAndAllowWhileIdle() methods from AlarmManager class as per your requirement for API level >= 23.
According to the documentation these are appropriate for notification purposes.
setAndAllowWhileIdle() documentation:
Like set(int, long, PendingIntent), but this alarm will be allowed to
execute even when the system is in low-power idle modes. This type of
alarm must only be used for situations where it is actually required
that the alarm go off while in idle -- a reasonable example would be
for a calendar notification that should make a sound so the user is
aware of it. When the alarm is dispatched, the app will also be added
to the system's temporary whitelist for approximately 10 seconds to
allow that application to acquire further wake locks in which to
complete its work.

Related

Alarm Manager or Work Manager for Recurring Background Work

I'm having trouble deciding whether to run some recurring background work with Alarm Manager or Work Manager:
The work is going to consist of Room Database operations so I'll need access to Dao to complete my work.
It is going to be recurring at fixed intervals (hourly, daily, weekly, monthly, etc.)
I need to set a start date and time for the recurrence intervals.
the work will recur until canceled by the user
If the user is using the app when the work is supposed to be scheduled, I want the work to be done immediately. If the user is not on the app (app is in the background or device is turned off), I don't care if the work is done after the scheduled time as long as it is at least started by the next time the user opens the app.
the work needs to continue as scheduled after device reboots and app restarts.
For recurring background work, AlarmManger isn't suitable. As the name implies, it's intended to notify the system of an event at a precise time. Just like a physical alarm that wakes a person up even if the person sleeps, AlarmManager will wake up the device from doze mode which will result in more power usage. it is suitable for suitations like setting remainders such as for calender events which the users probably set by themselves.
On the other hand, WorkManager is intended to carry out background processing or work that would persist. Workmanager is much more efficient for recurring task especially as it allows you set constraints to determine when it should start or stop the background work.
check the link form the offical documentation on WorkManger:
workmanager architecture
tabular comparison between the two
It mostly depends on how important your task is.
https://developer.android.com/guide/background is a really good entry point to help you choose what you should work with.
WorkManager is the modern, universal approach of handling background work and it fits for most use-cases, it automatically reschedules work after a device restart or an application crash, and it is very efficient in terms of battery usage.
As WorkManager does respect Android's doze mode, it does not guarantee, that the work will be done exactly on time, though it does guarantee, that your work will be done within a certain time frame.
On the other hand, AlarmManager is capable of running work precisely on time. But this means that the device will wake up when your work scheduled with AlarmManager is coming due. This will drain battery and your app will probably show up as battery-draining in the Device Health board.
But as stated in the article above, prefer using WorkManager if possible. AlarmManager should only be used for e.g. a time-sensitive calendar notification.

I want to schedule notification on specific date should i use workmanager?

I am developing one app where I want to schedule notification on a specific date and time.
Specifically on a date when the app trial is over. I am giving a 4-day app trial to the user and when the trial is over.
ex: the user installs the app on 05-03-2020 then the app will give notification on date 08-03-2020
so should I use the work manager to schedule notification or any other option in android?
WorkManager is intended for tasks that are deferrable - that is, not required to run immediately and required to run reliably even if the app exits or the device restarts. For example:
Sending logs or analytics to backend services
Periodically syncing application data with a server
Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.
Alarms have these characteristics:
They let you fire Intents at set times and/or intervals.
They operate outside of your application, so you can use them to
trigger events or actions even when your app is not running, and even
if the device itself is asleep.
So for your requirement, you should be using an AlarmManager instead of WorkManager as you only need to deliver a notification.

How to use alarm in Android Pie

i'm making an app for events and users can set alerts to the events they want. I use the AlarmManager for this, and the problem is on Android 9(Pie), that it seems blocking alarms and the notifications is not showing anymore.
For Android <= 8 there aren't problems.
Any tip/solution here?
Thanks
Alarms do not fire when the device is idle in Doze mode. Any scheduled alarms will be deferred until the device exits Doze. If you need to ensure that your work completes even when the device is idle there are several options available. You can use setAndAllowWhileIdle() or setExactAndAllowWhileIdle() to guarantee that the alarms will execute. Another option is to use the new Schedule tasks with WorkManager, which is built to perform background work either once or periodically. For more information, see Schedule tasks with Schedule tasks with WorkManager.
Ref: Android Developer.

How do you replace the AlarmManager working in the background in Android Oreo while still setting a specific time of day?

Short version: Alarm Managers no longer work in the background in Android 8 (Oreo), Android people are saying to use Job Scheduler as the alternative, but the job scheduler doesn't allow specific times of the day like Alarm Manager did so well.
Longer version: I have a "reminder" notification that goes off at a specific time of day set by the user. Up to this point, I've used an alarm manager to post the notification at the set time. This sends the user a notification to the user even if the app is in the background. But in Android Oreo / 8.0, this isn't allowed anymore. Most background services, receivers, etc. are fairly limited. I've set up a job scheduler, but doesn't allow for an exact time. Periodic settings in the job scheduler cannot be set ahead to a specific time. So despite Android's claims that job scheduler is the intended replacement for alarm manager, it doesn't seem to do one of the key things people use it for, setting at a specific time of the day. Does anyone know of a way to do this in Android Oreo in a way that it works even when the app is in the background?
I've tried sync adapters (need the alarm manager for that too!), handlers (nothing for a specific time), playing around with the different settings for the job service (periodic, minimumLatency, etc.), resetting the notification after the last one finished (can't do that in the background either!), and still haven't found a valid solution.
Is there anything that works like the job scheduler (i.e. Android 8/Oreo lets you run in the background, but that lets you schedule an exact time of the day, on a daily basis?
Alarm Managers no longer work in the background in Android 8 (Oreo)
I am not aware of anything regarding AlarmManager that changed in Android 8.0. Feel free to point to documentation or an issue tracker entry to validate your claim. Or, file your own issue with a reproducible test case.
But in Android Oreo / 8.0, this isn't allowed anymore.
Sure it is, at least as well as it worked in Android 6.0 through 7.1. Doze mode and app standby have screwed with AlarmManager, but that's not new to Android 8.0.
Most background services, receivers, etc. are fairly limited.
They can still raise a Notification, which is what your question indicates that you want to do.
Does anyone know of a way to do this in Android Oreo in a way that it works even when the app is in the background?
Use AlarmManager. Since you are writing what amounts to an alarm clock app, use setAlarmClock().
one solution i can suggest may be solved your problem.
you should start job schedule 15 minute repeater mode and call every minute one broadcast where you can check this current time is your specific time of day or not.
if yes then send notification.

Android - JobScheduler or BroadcastReceiver?

In my Android app I need to do some work every time the user plugs their device. For this purpose right now I use a BroadcastReceiver, which starts my IntentService to do the work when the user plugs the device and stops it when the device becomes unplugged.
Right now I'm thinking of using JobScheduler for Android 5.0+, but what I'm seeing is that with JobScheduler, I would have to schedule my job within the app, by calling
JobScheduler.schedule(JobInfo);
But this is a problem to me, because I want my job to run every time the user connects their device to the charger, even without the user having to open my app.
For this reason, I think one way would be to schedule it the first time the user opens the app, and then always force reschedule, since I cannot trust on the user opening my app every day (which, due to the nature of my app, certainly won't happen).
So, should I stick with BroadcastReceiver or use JobScheduler for Android 5.0+?
And in the case of using JobScheduler, should I schedule my job only once and then always return true in order to force rescheduling?
Thank you.
So, should I stick with BroadcastReceiver or use JobScheduler for Android 5.0+?
Use JobScheduler, this can improve your app’s performance, along with aspects of system health such as battery life. Also, JobScheduler persists through device reboots and supports batch scheduling by which the android system can combine pending jobs thus reducing battery usage. Moreover, you can do distinguish between android versions thus using JobScheduler on Lollipop and up, and AlarmManager on older versions.
And in the case of using JobScheduler, should I schedule my job only once and then always return true in order to force rescheduling?
Now, there are 2 ways to do this :
As you guessed, scheduling your job only once and always returning true in jobFinished() - this should do the trick.
Upon completing a job (originally scheduled by you by calling JobScheduler.schedule(JobInfo)), you schedule another job by calling the same. This will schedule consequent jobs once each job is about to be completed.
Jobscheduler runs in the background and persists through reboots so you should be fine.

Categories

Resources