Stop Flutter Background Service on App Termination

325 Views Asked by At

I'm using the 'flutter_background_service' to run some codes when my app is minimized or the phone screen is OFF.

I need the background service to be self-terminated or to force shut it down when the app is closed by the user (swipe up from Recent Apps view) because I do not want it drain phone battery and keep showing the notification.

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();
final bgServiceInstance = FlutterBackgroundService();

const AndroidNotificationChannel channel = AndroidNotificationChannel(
  notificationChannelId,
  notificationChannelName,
  description: notificationChannelDescription,
  importance: Importance.low,
  playSound: true,
  showBadge: true,
);

Future<void> initializeBackgroundService() async {

  /* SETUP NOTIFICATION CHANNEL */
  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(
        channel,
      );

  /* CONFIGURE BACKGROUND SERVICE */
  await bgServiceInstance.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: true,
      notificationChannelId: notificationChannelId,
      initialNotificationTitle: notificationTitle,
      initialNotificationContent: notificationContent,
      foregroundServiceNotificationId: notificationId,
    ),
    iosConfiguration: IosConfiguration(
      autoStart: true,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );
}


@pragma('vm:entry-point')
Future<void> onStart(ServiceInstance bgService) async {

  DartPluginRegistrant.ensureInitialized();

  if (bgService is AndroidServiceInstance) {
    bgService.on('setAsForeground').listen((event) {
      bgService.setAsForegroundService();
    });
    bgService.on('setAsBackground').listen((event) {
      bgService.setAsBackgroundService();
    });
  }
  bgService.on('stopService').listen((event) {
    bgService.stopSelf();
  });

  Timer.periodic(const Duration(seconds: 1), (timer) async {
    if (bgService is AndroidServiceInstance) {
      if (await bgService.isForegroundService()) {
        flutterLocalNotificationsPlugin.show(
          notificationId,
          notificationTitle,
          notificationContent,
          const NotificationDetails(
            android: AndroidNotificationDetails(
              notificationChannelId,
              notificationChannelName,
              icon: notificationIcon,
              ongoing: true,
            ),
          ),
        );
      }
    }
  });
}


@pragma('vm:entry-point')
Future<bool> onIosBackground(ServiceInstance bgService) async {
  WidgetsFlutterBinding.ensureInitialized();
  DartPluginRegistrant.ensureInitialized();
  
  if (bgService is AndroidServiceInstance) {
    bgService.on('setAsForeground').listen((event) {
      bgService.setAsForegroundService();
    });
    bgService.on('setAsBackground').listen((event) {
      bgService.setAsBackgroundService();
    });
  }
  bgService.on('stopService').listen((event) {
    bgService.stopSelf();
  });
  
  Timer.periodic(const Duration(seconds: 1), (timer) async {
    if (bgService is AndroidServiceInstance) {
      if (await bgService.isForegroundService()) {
        flutterLocalNotificationsPlugin.show(
          notificationId,
          notificationTitle,
          notificationContent,
          const NotificationDetails(
            android: AndroidNotificationDetails(
              notificationChannelId,
              notificationChannelName,
              icon: notificationIcon,
              ongoing: true,
            ),
          ),
        );
      }
    }
  });
  return true;
}

I have tried using 'AppLifecycleListener' Class for monitoring the App status, and when the status is "detached" I try to stop the background service (as shown below) but it is not being run correctly, and the reason as I checked online is that the Flutter engine is needed to communicate with the native code and the background service and this Flutter engine is being killed as soon as the app is in the 'detached' status.

void _onStateChanged(AppLifecycleState state) {
    if (state == AppLifecycleState.detached) {
      bgServiceInstance.invoke('stopService');
    }
  }

Any recommendation for workaround would be appreciated

0

There are 0 best solutions below