plus_plugins icon indicating copy to clipboard operation
plus_plugins copied to clipboard

[Question]: AlarmManager not fire on Xiaomi Note 8

Open rgvieira opened this issue 2 years ago • 3 comments

What is your question?

Doctor -v: • Flutter version 3.4.0-19.0.pre.83 on channel master at d:\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision d58bbd8986 (11 days ago), 2022-09-05 15:17:25 -0400 • Engine revision 263cf42f8a • Dart version 2.19.0 (build 2.19.0-171.0.dev) • DevTools version 2.17.0 • No issues found!

android_alarm_manager_plus 2.0.7 

@pragma('vm:entry-point') static Future callback() async { developer.log('Alarm fired!'); // This will be null if we're running in the background. uiSendPort ??= IsolateNameServer.lookupPortByName(isolateName); // print('uisendprt ' + uiSendPort.toString()); uiSendPort?.send(null); }

// Register the UI isolate's SendPort to allow for communication from the // background isolate. IsolateNameServer.registerPortWithName( port.sendPort, isolateName, );

runApp( SplashApp(), );

@override void initState() { _controller = TabController( length: 5, vsync: this, ); super.initState(); AndroidAlarmManager.initialize(); }

await AndroidAlarmManager.periodic( const Duration(hours: 24), //Do the same every 24 hours _oneShotAtTaskId, //Different ID for each alarm callback, allowWhileIdle: true, exact: true, wakeup: true, //the device will be woken up when the alarm fires startAt: DateTime( DateTime.now().year, DateTime.now().month, DateTime.now().day, int.parse(hourController.text), int.parse(minuteController.text)), rescheduleOnReboot: true, //Work after reboot );

Works fine at Motorola G5 device. But alarm do not fire on a Xiaomi Note 8.

With or without @pragma('vm:entry-point').

Debug version or release version.

rgvieira avatar Sep 16 '22 20:09 rgvieira

My only guess is that the os is killing your app to save battery, since it works on other devices.

Can you disable battery saving (or some similar option) for your app on the settings?

miquelbeltran avatar Sep 17 '22 19:09 miquelbeltran

Disabling battery saving the alarm fires makes it works fine.

  • requestpermission Permission.ignoreBatteryOptimizations
  • with @pragma('vm:entry-point') on callback function

The pluging documents says printHello will run even if the main app ends. Just if the app is running.

Do you know a way to screduller the alarmManager even if the app is not runnig?

rgvieira avatar Sep 18 '22 15:09 rgvieira

The way it works on the Android OS is that, if the app is killed by the user (e.g. swipe up, force kill, etc.) the alarms will not fire: https://stackoverflow.com/questions/11658129/android-alarms-cancelled-when-application-is-killed

I personally never liked this functionality because it is very unreliable, so I haven't used it, ever.

miquelbeltran avatar Sep 19 '22 06:09 miquelbeltran

@miquelbeltran I think this is not applicable in general, but depends on the OEM. On stock android, according to the official android docs, swipe up in the recent-apps screen does not force-stop the app (and killing background processes and alarms), but just kills the activities. This reflects experiences with common Samsung or Google devices. However, it is also commonly reported that chinese OEMs work differently/more strict, introducing problems with different background APIs like WorkManager, AlarmManager, JobScheduler etc.

@rgvieira Therefore, the easiest solution may be, despite UX unfriendly, to manually request Permission.ignoreBatteryOptimizations from the user.

Further possible problems: You are using .periodic with allowWhileIdle: true with a 24h delay. Maybe your alarms are not called in time (your description is unclear in this case - you only say "not working") due to doze mode. Please see Schedule Alarms or Understanding Doze Mode for details. Be aware that, despite the plugins documentation, allowWhileIdle: true is not applicable when using .periodic. See the plugins source code or the AlarmManager documentation for details.

Furthermore, be aware of #1149.

guewin avatar Oct 17 '22 23:10 guewin

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

github-actions[bot] avatar Jan 16 '23 00:01 github-actions[bot]