flutteralarm

(FLUTTER Android) flutter alarm manager not open app at the time when app is terminated


I'm make a flutter alarm app (with alarm_manager_plus). When the app call

AndroidAlarmManager.oneShot(Duration(seconds:10),...);

On foreground, it wroks well, but when app is terminated (or mobile screen off) the app couldn't launch.

How could I deal with it?

https://pub.dev/packages/android_alarm_manager_plus/example

I tried this example.


Solution

  • in order to use app services in background you need to use this plugin

    https://pub.dev/packages/flutter_background_service

    example code

    Future<void> main() async {
        WidgetsFlutterBinding.ensureInitialized();
        await initializeService();
    
        runApp(MyApp());
    }
    
    // this will be used as notification channel id
    const notificationChannelId = 'my_foreground';
    
    // this will be used for notification id, So you can update your custom notification with this id.
    const notificationId = 888;
    
    Future<void> initializeService() async {
      final service = FlutterBackgroundService();
    
      const AndroidNotificationChannel channel = AndroidNotificationChannel(
        notificationChannelId, // id
        'MY FOREGROUND SERVICE', // title
        description:
            'This channel is used for important notifications.', // description
        importance: Importance.low, // importance must be at low or higher level
      );
    
      final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
          FlutterLocalNotificationsPlugin();
    
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
              AndroidFlutterLocalNotificationsPlugin>()
          ?.createNotificationChannel(channel);
    
      await service.configure(
        androidConfiguration: AndroidConfiguration(
          // this will be executed when app is in foreground or background in separated isolate
          onStart: onStart,
    
          // auto start service
          autoStart: true,
          isForegroundMode: true,
    
          notificationChannelId: notificationChannelId, // this must match with notification channel you created above.
          initialNotificationTitle: 'AWESOME SERVICE',
          initialNotificationContent: 'Initializing',
          foregroundServiceNotificationId: notificationId,
        ),
        ...