cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
527 stars 625 forks source link

[screen_state: ^3.0.1]W/FlutterJNI(14854): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: screenStateEvents. Response ID: 23 #955

Open Zubairpv opened 2 months ago

Zubairpv commented 2 months ago

"Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++." After start a background service and closes the app every detection throwing this error i need work this in background too

code

 // Method to start listening to screen events
   Future startListening(String para) async {
     try {
     // Check if beneficiary data is available
      if (beneficiaryLocalService.box.hasData("beneficiary")) {
        debugPrint("nujmbjnj");
        // Retrieve beneficiary data from local storage
        BenefiiciaryModel benefiiciaryModel =
        beneficiaryLocalService.retrieveFromGetStorage();
      // Start noise service for detecting beneficiary activity

    // Subscribe to screen state events
    _screen.screenStateStream!.listen((event) {
      onData(event, benefiiciaryModel, para);
    });
    // Set iscanalert to true, indicating that alerts can be sent
    iscanalert = true;
  } else {
    // Debug print if beneficiary data is not available
    debugPrint(
        beneficiaryLocalService.box.hasData("beneficiary").toString());
  }
} on ScreenStateException catch (exception) {
  // Handle ScreenStateException if any
  debugPrint(exception.toString());
}

}

// Method to handle screen state events void onData(ScreenStateEvent event, BenefiiciaryModel benefiiciaryModel, String para) { debugPrint("ondata"); // If the screen is turned off if (event == ScreenStateEvent.SCREEN_OFF) { debugPrint('object'); // Update beneficiary inactivity details in the database beneficiaryDatabaseService.inactivityDetailsUpdate( benefiiciaryModel.memberUid, {"lastlockedtime": DateTime.now().toString()}); // Start the timer for the beneficiary startTimer(benefiiciaryModel, para); } // If the screen is unlocked else if (event == ScreenStateEvent.SCREEN_UNLOCKED) { try { // Cancel the timer if it's running timer == null ? () { debugPrint("nu"); } : timer!.cancel(); } catch (e) { // Handle any exceptions if occurred debugPrint(e.toString()); } debugPrint(timer!.tick.toString()); debugPrint('unloacked'); // Update beneficiary inactivity details in the database beneficiaryDatabaseService .inactivityDetailsUpdate(benefiiciaryModel.memberUid, { "lastunlockedtime": DateTime.now().toString(), "lastInactivityhours": timer == null ? 0 : timer!.tick }); } } //calling on background

// Function to initialize the background service Future initializeService() async { final service = FlutterBackgroundService();

// Configure background service for Android await service.configure( iosConfiguration: IosConfiguration(), androidConfiguration: AndroidConfiguration( onStart: onStart, isForegroundMode: true, autoStart: true, autoStartOnBoot: true, ), );

// Start the background service service.startService(); }

// Entry point for the background service @pragma('vm-entry-point') Future onStart(ServiceInstance service) async { // Ensure that Dart plugin is initialized DartPluginRegistrant.ensureInitialized();

// If the service is an instance of AndroidServiceInstance if (service is AndroidServiceInstance) { // Listen for events to set the service as foreground or background service.on('setAsForeground').listen((event) { service.setAsForegroundService(); }); service.on('setAsBackground').listen((event) { service.setAsBackgroundService(); }); }

// Listen for event to stop the service service.on('stopService').listen((event) { service.stopSelf(); });

// If the service is an instance of AndroidServiceInstance if (service is AndroidServiceInstance) { // Check if the service is running in foreground if (await service.isForegroundService()) { // Initialize Firebase and GetStorage await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform); await GetStorage.init();

  // Start listening for screen events in the background
  ScreenTimerServices screenTimerServices = ScreenTimerServices();
  await screenTimerServices.startListening("background");

  debugPrint('Background service started');
} else {
  debugPrint('Background service not in foreground');
}

} }

careconnect278 commented 1 month ago

@xjodoin i have same issue

careconnect278 commented 1 month ago

@thomasnilsson please check this