Flutter: Reschedule notification on reboot (Android)

59 Views Asked by At

Hey guys quick question regarding rebooting on android: I have an app that is supposed to schedule reminders. When I reboot my device, those notifications aren't scheduled anymore so I have to reschedule them. So I wrote a function for rescheduling . I am using the alarm manager plus with the bootListener to trigger a callback on restart. What do I have to consider to successfully callback on reboot?

import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:project_h2o/services/notification_service.dart';

Future<void> setupBootListener() async {
  NotificationService notificationService = NotificationService();
  await AndroidAlarmManager.initialize();

  // Set the delay to a minimum value as we only need to trigger the callback
  await AndroidAlarmManager.oneShot(
    Duration(seconds: 1), // Minimal delay
    0, // Unique identifier for the alarm
    notificationService.rescheduleNotifications,
    wakeup: true,
    rescheduleOnReboot: true,
  );
}

And in my main I call the setup

void main() async {
  await initializeDateFormatting();
  WidgetsFlutterBinding.ensureInitialized();
  await findSystemLocale();
  tz.initializeTimeZones();
  tz.setLocalLocation(tz.getLocation('Europe/Berlin'));
  NotificationService notificationService = NotificationService();
  await notificationService.initNotifications();
  await setupBootListener();
  runApp(MyApp());
}
1

There are 1 best solutions below

0
azom On

I don't know if you looked at this, but I had a similar problem. You need to go into the AndroidManifest.xml to write a few lines for the alarm manager to work and be able to be there on reboot

Look at this link in the get started section to do that

Android alarm manager plus documentation