jiusanzhou / flutter_notification_listener

Flutter plugin to listen for and interact with all incoming notifications for Android. 一个监听手机通知的插件。
https://pub.dev/packages/flutter_notification_listener
Other
40 stars 49 forks source link

Fixed "FlutterJNI detached from native C++" issue #46

Closed Droyder7 closed 7 months ago

Droyder7 commented 7 months ago

Fixes #18 , #35, #45

Issue Description:

when the app is closed from the recent app menu, and a notification is posted, it seems like the listener has a broken channel connection and the dart code does not run, and produces the error message : "Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter_notification_listener/bg_method"

Fix Details:

Incorporated JNI objects into the plugin code. JNI allows Java code to interact with applications and libraries written in other languages, such as C++. By managing the JNI lifecycle properly, we can avoid detachment issues and ensure a smooth communication flow between Flutter and native code

jiusanzhou commented 7 months ago

Hey @Droyder7 thank you for your great job, I have no idea how this issue happened. I will take a look.

Droyder7 commented 7 months ago

Hey @Droyder7 thank you for your great job, I have no idea how this issue happened. I will take a look.

No worries, I'm happy to help. Actually this issue can be reproduced in the example app itself, here are the steps:

  1. Run the app & Run flutter logs for checking logs
  2. Start listener in the app
  3. Remove the app from recent menu
  4. Send some notification / message in phone

It will crash after sending the NotificationEvent through the send port. For more details you can also check the attached issues.

Thanks for appreciating my contribution, please let me know if you need any other clarification in order to merge this PR.

Jace-Joji commented 7 months ago

@Droyder7 @jiusanzhou Hi, I am new to flutter. Would you mind explaining how to implement the fix? I need it for a project that I am working on

Droyder7 commented 7 months ago

@Droyder7 @jiusanzhou Hi, I am new to flutter. Would you mind explaining how to implement the fix? I need it for a project that I am working on

Hi @Jace-Joji , Ideally you have to wait for this fix to be merged in the main repo but for the mean time you can use my repo as source in flutter dependency. Just add the following lines in dependencies section in pubspec.yaml file of your flutter project:

  flutter_notification_listener:
    git:
      url: https://github.com/Droyder7/flutter_notification_listener.git
jesselpereira commented 7 months ago

@Droyder7 @jiusanzhou Hi, I am new to flutter. Would you mind explaining how to implement the fix? I need it for a project that I am working on

Hi @Jace-Joji , Ideally you have to wait for this fix to be merged in the main repo but for the mean time you can use my repo as source in flutter dependency. Just add the following lines in dependencies section in pubspec.yaml file of your flutter project:

  flutter_notification_listener:
    git:
      url: https://github.com/Droyder7/flutter_notification_listener.git

Hi, I'm using the package pointing to your repository but I'm still getting the error.

image

bhatakeed commented 7 months ago

@Droyder7 you package works fine in debug mode but not working in release mode. Not able to listen to notification when app is terminated in release mode. I get same error @jesselpereira

jesselpereira commented 7 months ago

@Droyder7 you package works fine in debug mode but not working in release mode. Not able to listen to notification when app is terminated in release mode. I get same error @jesselpereira

It's true, I didn't experience any hindrances while in debug mode, but when uploading to the Google Play Store I received notifications from Firebase Crashlytics.

It would be perfect if it were corrected.

Droyder7 commented 7 months ago

Hi @jesselpereira, Thanks for pointing out this issue, the destroy method your App's activity is crashing while terminating the app as the flutter engine should not be destroyed when the flutterJNI is attached to native.

So, we have to prevent the app from trying to destroy the flutter engine when terminating the app, some additional changes may needs to be done on the app side in order to fix this.

Overwrite the below mentioned code in the MainActivity.kt file of the android app :

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }

    override fun shouldDestroyEngineWithHost(): Boolean = false
}

Let me know if it solves the issue or not, Thanks.

jesselpereira commented 7 months ago

Hi @jesselpereira, Thanks for pointing out this issue, the destroy method your App's activity is crashing while terminating the app as the flutter engine should not be destroyed when the flutterJNI is attached to native.

So, we have to prevent the app from trying to destroy the flutter engine when terminating the app, some additional changes may needs to be done on the app side in order to fix this.

Overwrite the below mentioned code in the MainActivity.kt file of the android app :

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }

    override fun shouldDestroyEngineWithHost(): Boolean = false
}

Let me know if it solves the issue or not, Thanks.

I applied the change and it really worked, the exception disappeared! Thank you very much for the solution.

Please, if you are interested, could you take a look at #47 and tell me if this case is possible?

Prashant-Mohania commented 3 months ago
[ERROR:flutter/shell/common/shell.cc(117)] Dart Error: Dart_LookupLibrary: library 'package:flutter_notification_listener/src/plugin.dart' not found.
E/flutter (19660): [ERROR:flutter/runtime/dart_isolate.cc(677)] Could not resolve main entrypoint function.
E/flutter (19660): [ERROR:flutter/runtime/dart_isolate.cc(168)] Could not run the run main Dart entrypoint.
E/flutter (19660): [ERROR:flutter/runtime/runtime_controller.cc(462)] Could not create root isolate.
E/flutter (19660): [ERROR:flutter/shell/common/shell.cc(669)] Could not launch engine with configuration.

I am getting this error when I close the app and then background callback is not working, It's working fine in debug, error was in when we run the app in release mode Issue can we generate in example app

  1. Run the app & Run flutter logs for checking logs