firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.53k stars 3.93k forks source link

🐛 [Firebase_Messaging] Unable to use platform plugins during background message handling #5551

Closed makstheimba closed 3 years ago

makstheimba commented 3 years ago

Bug report

Describe the bug I successfully receive both foreground and background messages using firebase_messaging. However there is an issue with background messages after updating from firebase_core: ^0.5.0+1 & firebase_messaging: ^7.0.3 to firebase_core: ^1.0.2 & firebase_messaging: ^9.1.0. Every time I try to communicate with a plugin from background message handling I receive: [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: MissingPluginException(No implementation found for method ...

I suspect it has something to do with how plugins were registered previously (since it works fine prior to update).

I had this code inside inside App.kt ``` ... import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService import ru.yandex.telephony.telecom.SipProxy import ru.yandex.telephony.telecom.TelecomManagerPlugin class App : FlutterApplication(), PluginRegistry.PluginRegistrantCallback { override fun onCreate() { super.onCreate() ... FlutterFirebaseMessagingService.setPluginRegistrant(this) } override fun registerWith(registry: PluginRegistry) { TelecomManagerPlugin.registerWith(registry.registrarFor("ru.yandex.telephony.telecom.TelecomManagerPlugin")) SipProxy.registerWith(registry.registrarFor("ru.yandex.telephony.telecom.SipProxyService")) FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")) } } ```

I also have flutterEmbeddingV2 enabled. After update FirebaseMessagingPlugin and FlutterFirebaseMessagingService are no longer available, so if I just remove it how guide suggests, I receive MissingPluginException error described above.

I also tried to follow steps from guide to register FirebaseMessagingBackgroundService ``` package ru.yandex.telephony import com.yandex.passport.api.Passport import io.flutter.app.FlutterApplication import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugin.common.PluginRegistry import io.flutter.plugins.GeneratedPluginRegistrant import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService import ru.yandex.telephony.telecom.SipProxy import ru.yandex.telephony.telecom.TelecomManagerPlugin class App : FlutterApplication(), PluginRegistry.PluginRegistrantCallback { override fun onCreate() { super.onCreate() ... FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this) } override fun registerWith(registry: PluginRegistry) { GeneratedPluginRegistrant.registerWith(registry as FlutterEngine) TelecomManagerPlugin.registerWith(registry.registrarFor("ru.yandex.telephony.telecom.TelecomManagerPlugin")) SipProxy.registerWith(registry.registrarFor("ru.yandex.telephony.telecom.SipProxyService")) } } ```
Then whole application just fails with this error: ``` I/FLTFireBGExecutor( 7673): Creating background FlutterEngine instance, with args: [--start-paused, --enable-dart-profiling] W/FlutterEngineCxnRegstry( 7673): Attempted to register plugin (io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry$ShimRegistrarAggregate@732254e) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@bd66f). D/AndroidRuntime( 7673): Shutting down VM E/AndroidRuntime( 7673): FATAL EXCEPTION: main E/AndroidRuntime( 7673): Process: ru.yandex.mobile.telephony, PID: 7673 E/AndroidRuntime( 7673): java.lang.ClassCastException: io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry cannot be cast to io.flutter.embedding.engine.FlutterEngine E/AndroidRuntime( 7673): at ru.yandex.telephony.App.registerWith(App.kt:31) E/AndroidRuntime( 7673): at io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundExecutor.lambda$null$0$FlutterFirebaseMessagingBackgroundExecutor(FlutterFirebaseMessagingBackgroundExecutor.java:206) E/AndroidRuntime( 7673): at io.flutter.plugins.firebase.messaging.-$$Lambda$FlutterFirebaseMessagingBackgroundExecutor$e4cdQ2b1x-adsVcO-hA2dn2VM8E.run(Unknown Source:6) E/AndroidRuntime( 7673): at android.os.Handler.handleCallback(Handler.java:883) E/AndroidRuntime( 7673): at android.os.Handler.dispatchMessage(Handler.java:100) ```

Am I doing something wrong or is it no longer possible to communicate with platform channel from background message handling?

Steps to reproduce

Steps to reproduce the behavior:

  1. Install flutter 2 and set up firebase_messaging
  2. Try to communicate with any platform channel inside background message handling
  3. Receive the following error describing that no plugin is registered: [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: MissingPluginException(No implementation found for method ...

Expected behavior

I'm able to communicate with registered plugins from background message handling

Flutter doctor

Click To Expand ``` makstheimba@makstheimba-osx pbx_app % flutter doctor -v [✓] Flutter (Channel stable, 2.0.3, on Mac OS X 10.15.7 19H15 darwin-x64, locale en-US) • Flutter version 2.0.3 at /Users/makstheimba/flutter/flutter • Framework revision 4d7946a68d (2 weeks ago), 2021-03-18 17:24:33 -0700 • Engine revision 3459eb2436 • Dart version 2.12.2 [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at /Users/makstheimba/Library/Android/sdk • Platform android-30, build-tools 30.0.2 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.4, Build version 12D4e • CocoaPods version 1.10.0 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 4.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) [✓] VS Code (version 1.55.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.20.0 [✓] Connected device (2 available) • vivo 1907 (mobile) • 4PHIT4RSNNYPWK8P • android-arm64 • Android 10 (API 29) • Chrome (web) • chrome • web-javascript • Google Chrome 89.0.4389.90 • No issues found! ```

Flutter dependencies

Click To Expand ``` Dart SDK 2.12.2 Flutter SDK 2.0.3 pbx_app 2.3.0+2023 dependencies: - audioplayers 0.18.3 [uuid path_provider flutter flutter_web_plugins] - bloc 7.0.0 [meta] - cupertino_icons 1.0.2 - dart_date 1.1.0-nullsafety.0 [intl timeago] - device_info 2.0.0 [flutter device_info_platform_interface] - dotted_decoration 2.0.0 [flutter] - equatable 2.0.0 [collection meta] - firebase_core 1.0.2 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_messaging 9.1.0 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine] - flutter_bloc 7.0.0 [flutter bloc provider] - flutter_localizations 0.0.0 [flutter intl characters clock collection meta path typed_data vector_math] - flutter_ringtone_player 3.0.0 [flutter] - flutter_slidable 0.5.7 [flutter] - flutter_svg 0.19.3 [flutter meta path_drawing vector_math xml] - graphql 5.0.0-nullsafety.3 [meta path gql gql_exec gql_link gql_http_link gql_transform_link gql_error_link gql_dedupe_link hive normalize http collection web_socket_channel stream_channel rxdart uuid] - intl 0.17.0 [clock path] - is_lock_screen 1.0.0 [flutter] - logger 1.0.0 - mailto 1.1.0 [meta] - merge_map 1.0.2 - permission_handler 6.1.1 [flutter meta permission_handler_platform_interface] - phone_number 0.9.0 [flutter meta] - sentry 5.0.0 [http meta stack_trace uuid] - share 2.0.1 [meta mime flutter] - shared_preferences 2.0.5 [meta flutter shared_preferences_platform_interface shared_preferences_linux shared_preferences_macos shared_preferences_web shared_preferences_windows] - shimmer 1.1.2 [flutter] - sip_ua 0.3.5 [crypto flutter_webrtc intl logger parser_error path random_string recase sdp_transform uuid] - uni_links 0.4.0 [flutter] - url_launcher 6.0.3 [flutter url_launcher_platform_interface url_launcher_linux url_launcher_macos url_launcher_windows url_launcher_web] - uuid 3.0.3 [crypto] - visibility_detector 0.1.5 [flutter] dev dependencies: - bloc_test 8.0.0 [bloc meta mocktail test] - effective_dart 1.3.1 - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data] - lint 1.5.3 - mockito 4.1.4 [analyzer build code_builder collection dart_style matcher meta source_gen test_api] - pub_semver 2.0.0 [collection] - pubspec 2.0.1 [path pub_semver yaml uri] ```

atrope commented 3 years ago

@makstheimba Are you using V1 or V2 plugin in your background? also take a look at https://github.com/flutter/flutter/issues/75416

darshankawar commented 3 years ago

@makstheimba Please check above referred issue and see if it could be related to the behavior you are seeing. If not, it would be great if you could provide a complete minimal reproducible code sample that shows the issue. Thanks.

makstheimba commented 3 years ago

@atrope @darshankawar Yes, I'm using v1 embedding for the plugins that are expected to work in background.

I'll try to look at the issue you provided today, thanks. I think that may be the exact problem I'm having.

Once done I'll post results to this thread

makstheimba commented 3 years ago

@atrope @darshankawar As I've said I previously registered method channels for firebase to use in background

like this ``` class App : FlutterApplication(), PluginRegistry.PluginRegistrantCallback { override fun onCreate() { super.onCreate() ... FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this) } override fun registerWith(registry: PluginRegistry) { GeneratedPluginRegistrant.registerWith(registry as FlutterEngine) TelecomManagerPlugin.registerWith(registry.registrarFor("ru.yandex.telephony.telecom.TelecomManagerPlugin")) SipProxy.registerWith(registry.registrarFor("ru.yandex.telephony.telecom.SipProxyService")) } } ```

Those two plugins are only used in background, but at the same time they are heavily integrated with my app native kotlin code. I guess the only way is to somehow move those into a separate plugin. Because for some reason this plugins are not registered automatically by GeneratedPluginRegistrant, when they are declared inside my main android folder.

So provided link kinda helps, but I would like to find easier way so I don't need to move my plugins into separate package..

atrope commented 3 years ago

@makstheimba i would strongly recommend that you migrate those 2 plugins to use V2 embed :)

makstheimba commented 3 years ago

@atrope yeah, I understand, thank you! It was a mistake to heavily link those plugins with my kotlin code. Will try to figure out how to untangle all of those :)

I guess this issue should be closed now

atrope commented 3 years ago

@makstheimba You can still use them, just need to upgrade how they register Here is a guide: https://github.com/flutter/flutter/wiki/Experimental:-Create-Flutter-Plugin

Usually you just need to create 2 new functions and register the plugin inside those

makstheimba commented 3 years ago

@atrope I tried provided guide but it didn't work (may be it's a bit outdated or I'm too dumb). I'll dedicate some time later to migrate to v2 plugins instead. I think it'll work out. Thank you!

darshankawar commented 3 years ago

@atrope yeah, I understand, thank you! It was a mistake to heavily link those plugins with my kotlin code. Will try to figure out how to untangle all of those :)

I guess this issue should be closed now

Closing this issue based on the comment above.