Reputation: 3375
I am trying to make an alarm fire in my app every 30 minutes using Alarm Manager's setExactAndAllowWhileIdle
but it is not working!
I test the functionality by issuing a push notification whenever I receive an alarm signal.
The problem is: when the device enters doze mode after being idle for sometime, I no longer receive alarms. However, as soon as I turn On the screen, I receive a notification. My app needs accurate alarms that need to be delivered exactly on-time every 30 minutes! It can not afford to receive delayed alarms or dropped ones because the device is in Doze mode!
I used the following in my code:
onReceive()
method I set the next alarm. I also, start a
startWakefulService that only issues a push notification, then stops
itself.Notes:
android.permission.WAKE_LOCK
setAlarmClock()
which worked all the time even during doze
mode, with one dropped/delayed alarm every 50 alarms. So, it is also not
perfect. And I don't want the user to see an alarm icon all the time up
there.P.S: Before answering please make sure you are aware of the restrictions imposed starting Android 6.0 M and Doze mode.
Link1: https://developer.android.com/training/monitoring-device-state/doze-standby.html
In summary it says this:
Now, why don't I get accurate alarm signals every 30 minutes using setExactAndAllowWhileIdle()
?!
And, why isn't setAlarmClock()
100% reliable?!
Upvotes: 26
Views: 9979
Reputation: 2883
In case you want to show a notification each time the timer fires, then you can use a backend server to send a data notification to Android users.
You need to save the FCM tokens of Android clients in your backend server, and the server will be responsible for pushing notifications to those clients.
I know It's a bit complicated for just only showing a notification, but that's who the Android system works!
Upvotes: 0
Reputation: 38147
You might get accurate alarms, but in Doze mode
The system ignores wake locks.
So it seems as though AlarmManager.setAlarmClock
is the only acceptable solution if you really need to trigger every 30 minutes. This will probably negate all doze mode energy savings...
BTW: it seems like you can see alarms with adb shell dumpsys alarm
.
Possibility: use Firebase JobDispatcher
The Firebase JobDispatcher is a library for scheduling background jobs in your Android app. It provides a JobScheduler-compatible API that works on all recent versions of Android (API level 14+) that have Google Play services installed.
Upvotes: 6
Reputation: 591
Have you tried add your app to the battery optimisation whitelist? As some news pointed out (link), Huawei has some special battery management.
Upvotes: 0
Reputation: 26
I got the same problem as you, and searches a long time for an solution. But I doesn't find a general solution.
A solution for Samsung devices is available: Android AlarmManager not working on some devices when the app is closed
The first answer works not, but the second one :)
Upvotes: 0