1 year ago

#367433

test-img

Tim

Flutter android_alarm_manager_plus does not work reliably

I want to schedule daily notifications at a specific daytime with the android_alarm_manager_plus and the local_notifications plugins. However, the notifications come in very irregulary. The delay of the notifications increases each consecutive day. For example, the app should send a reminder everyday at 8:00 am. The first day, the notification comes in at around the right time. The next day, it sends it 15 minutes delayed and the following days, no notification gets send at all.

I tested this on a Motorola Moto G9 Play with Android 11 and I have deactivated the energy-saving mode and made sure that battery-optimizations are turned off for this app.

In the following I created a litte (hopefully) reproducable example:

// initializing similar to the official example
Future<FlutterLocalNotificationsPlugin> _initNotificationsPlugin() async {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('flutter_logo');
  final IOSInitializationSettings initializationSettingsIOS =
      IOSInitializationSettings();
  final MacOSInitializationSettings initializationSettingsMacOS =
      MacOSInitializationSettings();
  final InitializationSettings initializationSettings = InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS,
      macOS: initializationSettingsMacOS);
  await flutterLocalNotificationsPlugin.initialize(initializationSettings);
  return flutterLocalNotificationsPlugin;
}

Future<void> scheduleNotifications() async {
  int id = 0;
  AndroidAlarmManager.periodic(Duration(days: 1), id, sendNotification,
      exact: true,
      allowWhileIdle: true,
      rescheduleOnReboot: true,
      wakeup: true);
  log("Notifications scheduled");
}

// Top-level callback for the AndroidAlarmManager
Future<void> sendNotification() async {
  FlutterLocalNotificationsPlugin flutterNotificationsPlugin =
      await _initNotificationsPlugin();
  await flutterNotificationsPlugin.cancelAll();

  final now = DateTime.now();
  int id =
      now.second + now.minute * 60 + now.hour * 60 * 60; // Daytime in seconds

  // Copied from the example with edited channel id, etc.
  const AndroidNotificationDetails androidPlatformChannelSpecifics =
      AndroidNotificationDetails(
          'my very own channel id', 'my very own channel name',
          channelDescription: 'my channel description',
          importance: Importance.max,
          priority: Priority.high,
          ticker: 'ticker');
  const NotificationDetails platformChannelSpecifics =
      NotificationDetails(android: androidPlatformChannelSpecifics);
  //

  final body = "Notification sent at ${now.hour}:${now.minute}:${now.second}";
  await flutterNotificationsPlugin.show(
      id, "Test notification", body, platformChannelSpecifics);
  log(body);
}

Future<void> cancelAllAlarmsAndNotifications() async {
  await AndroidAlarmManager.cancel(0);
}

And when the user clicks a button, the following code is executed:

onPressed: () {
            cancelAllAlarmsAndNotifications().then((value) {
              scheduleNotifications();
            });
          },

Also, the main method:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  AndroidAlarmManager.initialize().then((value) {
    runApp(const MyApp());
  });
}

I really hope that you see some mistake I make that is easy fixable :)

Thanks in advance!

android

flutter

android-notifications

alarmmanager

android-alarms

0 Answers

Your Answer

Accepted video resources