-
Notifications
You must be signed in to change notification settings - Fork 4k
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
🐛 [firebase_messaging] setting onBackgroundMessage handler breaks plugin platform channel message sending from Android #9689
Comments
Thank for the detailed report @umbrellait-timur-khismatullin |
@darshankawar hello, Log with FirebaseMessaging.onBackgroundMessage
Without
Probably these lines from the log will give additional information:
I think this is a different problem from the #5551 |
Thanks for the update. Based on the details shared and the behavior being replicable using sample project shared, I am keeping this issue open for further insights from the team on expected behavior in the use case described. /cc @russellwheatley |
@darshankawar @russellwheatley On Android either Background (Minimized) or Terminated (Closed) notifications are not working; in DEBUG mode all works, when we deploy, Background and Terminated doesn't work... |
@ViniciusSossela You are probably hitting #9446 which has been fixed and solution is as mentioned here: #9446 (comment) |
Thanks @darshankawar . This issue should be released. Still didn't found the proposed solution in the docs: Thanks |
Hey @kevinthecheung, it might be worth releasing a new version of the documentation at some point so users have the latest information for the above noted issue. Thanks. |
@russellwheatley Sure thing, done. |
I'm working on mindbox plugin. Some users also use firebase_messaging for their own purposes. But, as I said above, setting After the investigation, I can say that it looks like the plugin is being recreated when Is it possible to avoid unexpected plugin behavior when setting Click To Expand
|
Hi, do anyone find the solution to solve this problems. |
Facing this too. I can call android methods from dart, but not vice versa( |
@darshankawar It's been 3 months and the issue is still open without progress. Need an ETA of the fix or share any workaround. @umbrellait-timur-khismatullin Did you find any solution which works well? |
@shivamsharma2710 Firebase documentation says:
Isolate created an another flutter engine for himself and register all plugins (call onAttachedToEngine in your Plugin android class). In this case, the problem was that a static field was used in Plugin and had one instance of MethodChannel for several instances of flutter engine. |
@umbrellait-nikolay-silantyev Yes, I get it. I've tried this dirty hack which only fixes in foreground. For background, what did you do? |
I've refactored code and made channel field non-static (made as private field of TestPlugin) |
@umbrellait-nikolay-silantyev . Did that solved this issue? On making the channel field non static. I have channel field non static only. It didn't work for me. Link . For me still platform channels breaks with non static channel field in Plugin class. |
@umbrellait-nikolay-silantyev Your solution didn't work in my project. I've tried your repo as well and it also has an issue in background state. |
@darshankawar Still waiting for your reply on ETA or any workaround. |
firebase/flutterfire#9689 Made `channel` a regular field (non-static) and only set `backgroundChannel` (a static field) if it is null. This should keep the first `backgroundChannel` alive. Note that the `backgroundChannel` has to be static because it is called from the DownloadWorker, and it has no reference to the plugin.
firebase/flutterfire#9689 Made `channel` a regular field (non-static) and only set `backgroundChannel` (a static field) if it is null. This should keep the first `backgroundChannel` alive. Note that the `backgroundChannel` has to be static because it is called from the DownloadWorker, and it has no reference to the plugin.
Bumping this issue as well, used this suggestion but still does not work, its been months and this issue still persists. |
1 similar comment
Bumping this issue as well, used this suggestion but still does not work, its been months and this issue still persists. |
…breaks plugin's platform channel for sending method call from Android. Issue has been fixed by creating the two method-channels in plugin's android class. One static method channel is reserved for Android to Dart communication and other is for Dart to Android communication. Fixes, firebase/flutterfire#9689
Hey @umbrellait-timur-khismatullin, I've investigated this issue. What I think is happening:
Step 2 is essential for the function of the background message handler, there is no way around it. If the context is changed in the intervening period for a delayed function tethered to the previous context, the message back to the Flutter side is going to get lost. For example, If I remove the delayed execution function wrapper, it works. There isn't anything for FlutterFire to do as this is something for developers to be aware of when designing their app implementation. It is perhaps a limitation of the FlutterEngine if anything. |
@russellwheatley As you mentioned, the issue is not only at the time of intervening period. When FlutterBackground Message Handler is added, the method channel callbacks from native to android is totally breaking. You can checkout my reproducible example , In the MainActivity.kt onNewIntent() , I am sending callback to flutter. This callback never reaches the flutter as method channel is broken because flutter engine got re initialised But flutter to native method channel communication is working. Only native to flutter is broken. If this is a limitation with flutter engine itself, is there any alternative work around? As you can see many people having this issue, at least providing a dirty hack to make it temporarily work would be helpful. |
@kkgowtham-engg-sdk - Is there not a lifecycle hook you can use to reinitialise the background handler every time the FlutterEngine is reinitialised? example |
@russellwheatley I dont understand why this was closed, can someone guide me how to fix it? What should be done on the library side to avoid the channel breakup? |
@danielgomezrico - have you tried using the lifecycle hook? |
@russellwheatley can you elaborate more on that idea? I don't catch what do you mean 🙏🏻 |
The FlutterEngine is being reinitialised when the background message handler callback is registered. You need to reinitialise your plugin via onAttachedToEngine. This lifecycle hook is part of the Anything (e.g event listener) associated with the application context needs to be reinitialised in that method. Otherwise, you will lose the ability to communicate with the dart side. |
Thanks for the update, now I was able to see the |
@danielgomezrico - that doesn't sound right. It is definitely called twice on android when using the firebase messaging plugin & you have a background message handler setup in your Flutter app. |
@russellwheatley . Even after reinitializing , it's not working. Only if call platform channel with MethodChannel instance created First time,it is working. When reinitialed due to onattachEngine called twice and when I call flutter method through MethodChannel instance created second time by Firebase.Onbackground message, it is not working. Only I am able to communicate through flutter through instance created first time. So I made a dirty hack to save method channel instances in static hashSet and add method channel instances to it when onattachEngine is called every time .When I need to emit an event to flutter , I will call the same method with all MethodChannel instances, to avoid in future if another plugin like uses background isolates like flutter fire. The thing I need to know is that, will it send multiple event if I call same function with different MethodChannel instances. I know its a dirty work around. If you had better workaround please share with an example and some more explanation would be helpful |
Bug report
If onBackgroundMessage handler is set, functionality of all other plugins will be broken. This affects sending messages from Android to Flutter.
Steps to reproduce
Steps to reproduce the behavior:
Sample project
https://github.com/umbrellait-timur-khismatullin/on_background_message
Additional context
Probably it's the Flutter bug, similar issue: flutter/flutter#23904
Probably there is also a problem with the implementation of firebase_messaging plugin on the Android side :
https://github.com/firebase/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/android/src/main/java/io/flutter/plugins/firebase/messaging/FlutterFirebaseMessagingBackgroundExecutor.java#L141
Flutter doctor
Run
flutter doctor
and paste the output below:Click To Expand
Flutter dependencies
Run
flutter pub deps -- --style=compact
and paste the output below:Click To Expand
The text was updated successfully, but these errors were encountered: