[Bug]: Unreliability of plugin callback execution during doze mode
Platform
8+
Plugin
android_alarm_manager_plus
Version
All versions since 0.4.0
Flutter SDK
3.3.2 (but irrelevant)
Description
When using oneShot with alarmClock: true and allowWhileIdle: true the dart callback execution is still unreliable on Android O and later during doze mode due to the usage of JobIntentService.
This means, that the callback is often executed delayed (7-15mins) or sometimes is not executed at all.
JobIntentService does not promise immediate or reliable execution. Refer to the android documentation for details JobIntentService.
This problem was already mentioned in #58263.
JobScheduler is not allowed to run while Doze Mode, refs: DozeMode Restrictions
It is also mentioned in the official android documentation of AlarmManager that starting a service is unreliable: If your alarm receiver called "Context.startService()", it is possible that the phone will sleep before the requested service is launched.
A wake lock is only given by BroadcastReceiver till the end of its onReceive method, which possibly end before the JobIntentService may be even started.
The AlarmManager API is intended for cases where you want to have your application code run at a specific time, even if the application is not running, including reboots and especially during doze mode, being the only (best) way currently to have reliable offline notifications.
Due to the usage of JobIntentService, the options allowWhileIdle and alarmClock are bypassed and fairly useless.
Steps to reproduce
- Start android emulator/device with min. Android O, e.g. with API 28
- Build and deploy
AndroidAlarmManagerPlusexample app in release mode with adjustments from sectionCode Sample(usingalarmClock: trueandallowWhileIdle: true) - Deactivate device battery loading by
adb shell dumpsys battery unplug - Force doze mode by
adb shell dumpsys deviceidle enable&adb shell dumpsys deviceidle force-idle - Check that doze idle is active by running
adb shell dumpsys deviceidle get deepand printsIDLE - Set alarm clock
- Wait for the trigger time to pass - nothing happens - no alarm is triggered
- Call
adb shell dumpsys deviceidle step deepto switch manually to a doze mode maintenance window - the alarm is triggered
Optional: Log entry/exit points of the methods BroadcastReceiver#onReceive and AlarmService#onHandleWork.
Result: Logging reveals, that the alarm still goes off the right time, meaning that BroadcastReceiver#onReceive is called, but yet the dart callback is not triggered due to active doze mode (idle), meaning that AlarmService#onHandleWork is not called.
Solution Proposals
a) Give up on a work schedule approach and execute the callback directly, meaning calling a static method directly in the BroadcastReceiver, containing the logic from AlarmService#onCreate & AlarmService#onHandleWork.
Following the purpose of the AlarmManager API, I think there is no need to use a resource protecting way.
b) Migrating AlarmService from using deprecated JobIntentService to the now recommended WorkManager API.
Regarding to the documentation execution should be ensured, as well as scheduling immediate ("expedited") work.
This should at least ensure reliable execution, but may still make problems with doze mode.
Code Sample
Example app from this package, with tweaked onShot call, including alarmClock: true and allowWhileIdle: true:
await AndroidAlarmManager.oneShot(
const Duration(minutes: 1),
Random().nextInt(pow(2, 31) as int),
callback,
exact: true,
wakeup: true,
alarmClock: true,
allowWhileIdle: true,
);
Flutter Doctor
[√] Flutter (Channel stable, 3.3.2, on Microsoft Windows [Version 10.0.19044.2130], locale de-AT)
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision e3c29ec00c (5 weeks ago), 2022-09-14 08:46:55 -0500
• Engine revision a4ff2c53d8
• Dart version 2.18.1
• DevTools version 2.15.0
[√] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
• Platform android-33, build-tools 32.0.0
• Java version openjdk version "11.0.13" 2021-10-19
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[√] Android Studio (version 2021.2)
• Flutter plugin version 70.0.2
• Dart plugin version 212.5744
• Java version openjdk version "11.0.12" 2021-07-20
[√] Android Studio (version 2021.3)
• Flutter plugin version 70.2.3
• Dart plugin version 213.7433
• Java version openjdk version "11.0.13" 2021-10-19
[√] VS Code, 64-bit edition (version 1.68.1)
• VS Code at C:\Program Files\Microsoft VS Code
• Flutter extension version 3.42.0
[√] Connected device (3 available)
• Android SDK built for x86 64 (mobile) • emulator-5554 • android-x64 • Android 9 (API 28) (emulator)
• Chrome (web) • chrome • web-javascript • Google Chrome 106.0.5249.119
• Edge (web) • edge • web-javascript • Microsoft Edge 106.0.1370.42
[√] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
Checklist before submitting a bug
- [X] I Google'd a solution and I couldn't find it
- [X] I searched on StackOverflow for a solution and I couldn't find it
- [X] I read the README.md file of the plugin
- [X] I'm using the latest version of the plugin
- [X] All dependencies are up to date with
flutter pub upgrade - [X] I did a
flutter clean - [X] I tried running the example project
There's already a plugin for WorkManager by the flutter community: https://pub.dev/packages/workmanager We don't plan to migrate this plugin to WorkManager, the idea is to expose the Android Alarm Manager functionality to Flutter (somehow), if WorkManager works better for your usecase, I highly suggest you to use the other plugin.
As far as I know, there have been changes in Flutter 3 that lead to the plugin not working correctly, and has been mentioned on other issues, e.g. here: https://github.com/fluttercommunity/plus_plugins/issues/1077
does something speak against calling executeDartCallbackInBackgroundIsolate directly in the BroadcastReceiver when using alarmClock: true?
I don't know enough about this to have an opinion, sorry.
As @miquelbeltran already replied about WorkManager package existence from my side I can add info about recent updates that might help with oneShot() not working (note, it has nothing to do with issues with trigger time): https://github.com/fluttercommunity/plus_plugins/pull/1229
I think we can close it for now as oneShot works.
The description was updated and steps for reproduction added.
No, this issues is not related to #1077.
@vbuberen Please consider to reopen this issue. oneShot is not working as expected during doze mode and like the corresponding method from the AlarmManager API.
@miquelbeltran The section Solution Proposals should clearify what I meant with "migrating to WorkManager API".
I am not sure what you mean with the WorkManager API, feel free to submit a PR with the proposed solution.
same
allowWhileIdle: true does nothing to fire in doze mode
Same issue. The callback is delayed.