Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[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 this issue May 2, 2024 · 2 comments
Labels
bugfix a bug fix

Comments

@Zubairpv
Copy link

Zubairpv commented May 2, 2024

"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');
}

}
}

@Zubairpv Zubairpv added the bugfix a bug fix label May 2, 2024
@Zubairpv Zubairpv changed the title screen_state: ^3.0.1 [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 May 10, 2024
@careconnect278
Copy link

@xjodoin i have same issue

@careconnect278
Copy link

@thomasnilsson please check this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix a bug fix
Projects
None yet
Development

No branches or pull requests

2 participants