Flutter flutter_background_service not start always when app is in killed state

38 Views Asked by At

Sometime background service start if app is in killed state, but sometimes background service stop with error: Flutter engine disconnected. Connected engine count 0

Main function

void main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await initializeService();
}

Start Background Service if app in killed state

 @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);\
    if (state == AppLifecycleState.resumed) {
      print('_isInForeground');
    } else if (state == AppLifecycleState.paused) {
      print('_isInBackground');
    } else if (state == AppLifecycleState.detached) {
      print('_isInKilled');
      serviceBackground.startService();
    }
  }

Flutter background service page

Future<void> initializeService() async {
  final serviceBackground = FlutterBackgroundService();
  AndroidNotificationChannel channel = const AndroidNotificationChannel(
    "App",
    "foreground service",
    importance: Importance.high,
  );
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  await serviceBackground.configure(
      iosConfiguration: IosConfiguration(),
      androidConfiguration: AndroidConfiguration(
        onStart: onStart,
        isForegroundMode: true,
        autoStart: true,
        notificationChannelId: "App",
        initialNotificationTitle: "foreground service",
        initialNotificationContent: "initializing",
        foregroundServiceNotificationId: 888,
      ));
  serviceBackground.startService();
}

@pragma('vm-entry-point')
Future<void> onStart(ServiceInstance serviceBackground) async {
  WidgetsFlutterBinding.ensureInitialized();
  DartPluginRegistrant.ensureInitialized();
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  serviceBackground.on('stopService').listen((event) {
    print("stopService : $event");
    serviceBackground.stopSelf();
  });

  if (serviceBackground is AndroidServiceInstance) {
    if (await serviceBackground.isForegroundService()) {
      Geolocator.getPositionStream(
          locationSettings: const LocationSettings(
        accuracy: LocationAccuracy.high,
        distanceFilter: 20,
      )).listen((Position? position) {
        addItem(position?.latitude, position?.longitude);
      });

      flutterLocalNotificationsPlugin.show(
        888,
        "Background ON",
        "Location Tracked",
        const NotificationDetails(
            android: AndroidNotificationDetails(
          "App",
          "foreground service",
          ongoing: true,
        )),
      );
    }
  }
}

I am unable to find the issue as worker starts sometimes but not always.

0

There are 0 best solutions below