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.66k stars 3.96k forks source link

🐛 [flutter_messaging] Unable to run native code from background handler with MethodChannels #6309

Closed paulhobbel closed 3 years ago

paulhobbel commented 3 years ago

Bug report

I'm trying to invoke a own made MethodChannel from flutter_messaging's background handler. However when I do so I get the following exception:

E/flutter (26653): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: MissingPluginException(No implementation found for method init on channel com.asamco.argus.doorbell/call_methods)
E/flutter (26653): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:7)
E/flutter (26653): <asynchronous suspension>
E/flutter (26653): #1      NativeCallPlatform.setup (package:argus_doorbell/app/services/call/native/call.api.dart:30:5)
E/flutter (26653): <asynchronous suspension>
E/flutter (26653): 
E/flutter (26653): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: MissingPluginException(No implementation found for method displayIncomingCall on channel com.asamco.argus.doorbell/call_methods)
E/flutter (26653): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:7)
E/flutter (26653): <asynchronous suspension>
E/flutter (26653): #1      NativeCallPlatform.displayIncomingCall (package:argus_doorbell/app/services/call/native/call.api.dart:34:5)
E/flutter (26653): <asynchronous suspension>
E/flutter (26653): 

From my understanding I should bind my MethodChannel to the BinaryMessenger of the FlutterEngine. In this case that should be the FlutterEngine used by the background executor of flutter_messaging. Maybe the API could add a callback that can be listened to on Android/iOS to interact with the FlutterEngine

Steps to reproduce

Steps to reproduce the behavior:

  1. Register a background handler
  2. Try to invoke a own made MethodChannel
  3. See the missing plugin exception

Expected behavior

Instead it should be possible to register methodchannels to the flutter_messaging background executor's flutterengine

Sample project

Simplified version of what I do:

Future<void> _onBackgroundMessageReceived(RemoteMessage message) async {
  log("received background message");
  const MethodChannel _methodChannel = MethodChannel('com.asamco.argus.doorbell/call_methods');

  await _methodChannel.invoke('init');
  await _methodChannel.invokeMethod('displayIncomingCall', <String,dynamic>{
    'callId': 1,
    'displayName': 'Someone'
  });

}
class MainActivity: FlutterActivity(), MethodCallHandler {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "com.asamco.argus.doorbell/call_methods")
        methodChannel.setMethodCallHandler(this)
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
      when(call.method) {
        "init" -> {
          result.success(null)
        },
        "displayIncomingCall" -> {
          result.success(null)
        }
      }
    }
}

Additional context

Android: 10 Flutter: 2.2.1 Flutter Messaging: 9.1.1


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` [✓] Flutter (Channel stable, 2.2.1, on macOS 11.4 20F71 darwin-x64, locale en-GB) [!] Android toolchain - develop for Android devices (Android SDK version 30.0.2) ✗ Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/macos#android-setup for more details. [✓] Xcode - develop for iOS and macOS [✓] Chrome - develop for the web [✓] Android Studio (version 4.2) [✓] IntelliJ IDEA Ultimate Edition (version 2020.3.2) [✓] VS Code (version 1.56.2) [✓] Connected device (2 available) ```

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand ``` Dart SDK 2.13.1 Flutter SDK 2.2.1 argus_doorbell 1.0.0+1 dependencies: - cryptography 2.0.1 [collection crypto fixnum js meta typed_data] - cupertino_icons 1.0.2 - device_info 2.0.0 [flutter device_info_platform_interface] - firebase_core 1.0.3 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_messaging 9.1.1 [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_local_notifications 5.0.0+1 [flutter flutter_local_notifications_platform_interface platform timezone] - flutter_webrtc 0.6.3 [flutter] - http 0.13.1 [http_parser meta path pedantic] - json_annotation 4.0.1 - jwt_decode 0.3.1 - shared_preferences 2.0.5 [meta flutter shared_preferences_platform_interface shared_preferences_linux shared_preferences_macos shared_preferences_web shared_preferences_windows] - signalr_core 1.1.0 [http logging meta tuple web_socket_channel sse_client equatable] - stacked 2.0.2 [flutter get_it meta provider collection] - stacked_services 0.8.2 [flutter get] - web_socket_channel 2.0.0 [async crypto stream_channel] dev dependencies: - build_runner 1.12.2 [args async build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style glob graphs http_multi_server io js logging meta mime path pedantic pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml] - 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] - json_serializable 4.1.0 [analyzer build build_config collection json_annotation meta path source_gen] transitive dependencies: - _fe_analyzer_shared 19.0.0 [meta] - analyzer 1.3.0 [_fe_analyzer_shared cli_util collection convert crypto glob meta package_config path pub_semver source_span watcher yaml pedantic] - args 2.0.0 - async 2.5.0 [collection] - boolean_selector 2.1.0 [source_span string_scanner] - build 2.0.0 [analyzer async convert crypto glob logging meta path] - build_config 0.4.7 [checked_yaml json_annotation meta path pubspec_parse yaml] - build_daemon 2.1.10 [built_collection built_value http_multi_server logging pedantic path pool shelf shelf_web_socket stream_transform watcher web_socket_channel] - build_resolvers 2.0.0 [analyzer build crypto graphs logging path package_config pool pub_semver stream_transform] - build_runner_core 6.1.12 [async build build_config build_resolvers collection convert crypto glob graphs logging meta path package_config pedantic pool timing watcher yaml] - built_collection 5.0.0 - built_value 8.0.4 [built_collection collection fixnum] - characters 1.1.0 - charcode 1.2.0 - checked_yaml 2.0.1 [json_annotation source_span yaml] - cli_util 0.3.0 [meta path] - clock 1.1.0 - code_builder 3.7.0 [built_collection built_value collection matcher meta] - collection 1.15.0 - convert 3.0.0 [typed_data] - crypto 3.0.0 [collection typed_data] - dart_style 2.0.0 [analyzer args path pub_semver source_span] - device_info_platform_interface 2.0.1 [flutter meta plugin_platform_interface] - equatable 2.0.0 [collection meta] - fake_async 1.2.0 [clock collection] - ffi 1.0.0 - file 6.1.0 [meta path] - firebase_core_platform_interface 4.0.0 [flutter meta plugin_platform_interface] - firebase_core_web 1.0.2 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_messaging_platform_interface 2.1.1 [firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 1.0.4 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] - fixnum 1.0.0 - flutter_local_notifications_platform_interface 3.0.0 [flutter plugin_platform_interface] - flutter_web_plugins 0.0.0 [flutter js characters collection meta typed_data vector_math] - get 4.1.3 [flutter] - get_it 6.0.0 [async collection] - glob 2.0.0 [async collection file path pedantic string_scanner] - graphs 1.0.0 - http_multi_server 2.2.0 [async] - http_parser 4.0.0 [charcode collection source_span string_scanner typed_data] - io 0.3.5 [meta path string_scanner] - js 0.6.3 - logging 1.0.0 - matcher 0.12.10 [stack_trace] - meta 1.3.0 - mime 1.0.0 - nested 1.0.0 [flutter] - package_config 2.0.0 [path] - path 1.8.0 - path_provider_linux 2.0.0 [path xdg_directories path_provider_platform_interface flutter] - path_provider_platform_interface 2.0.1 [flutter meta platform plugin_platform_interface] - path_provider_windows 2.0.0 [path_provider_platform_interface meta path flutter ffi win32] - pedantic 1.11.0 - platform 3.0.0 - plugin_platform_interface 2.0.0 [meta] - pool 1.5.0 [async stack_trace] - process 4.2.1 [file path platform] - provider 5.0.0 [collection flutter nested] - pub_semver 2.0.0 [collection] - pubspec_parse 1.0.0 [checked_yaml collection json_annotation pub_semver yaml] - quiver 3.0.0 [matcher] - shared_preferences_linux 2.0.0 [flutter file meta path path_provider_linux shared_preferences_platform_interface] - shared_preferences_macos 2.0.0 [shared_preferences_platform_interface flutter] - shared_preferences_platform_interface 2.0.0 [flutter] - shared_preferences_web 2.0.0 [shared_preferences_platform_interface flutter flutter_web_plugins meta] - shared_preferences_windows 2.0.0 [shared_preferences_platform_interface flutter file meta path path_provider_platform_interface path_provider_windows] - shelf 1.1.0 [async collection http_parser path stack_trace stream_channel] - shelf_web_socket 0.2.4+1 [shelf stream_channel web_socket_channel] - sky_engine 0.0.99 - source_gen 1.0.0 [analyzer async build dart_style glob meta path pedantic source_span] - source_span 1.8.0 [charcode collection path term_glyph] - sse_client 0.1.0 [http stream_channel] - stack_trace 1.10.0 [path] - stream_channel 2.1.0 [async] - stream_transform 2.0.0 - string_scanner 1.1.0 [charcode source_span] - term_glyph 1.2.0 - test_api 0.2.19 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher] - timezone 0.7.0 [path] - timing 0.1.1+3 [json_annotation] - tuple 2.0.0 [quiver] - typed_data 1.3.0 [collection] - vector_math 2.1.0 - watcher 1.0.0 [async path pedantic] - win32 2.0.4 [ffi] - xdg_directories 0.2.0 [meta path process] - yaml 3.1.0 [collection source_span string_scanner] ```

markusaksli-nc commented 3 years ago

Hi @paulhobbel As far as I know, it isn't possible to directly call a MethodChannel from an isolate in dart. It should be possible to get around this if you make your own plugin instead https://flutter.dev/docs/development/packages-and-plugins/developing-packages#plugin

This is a limitation of Dart, not FlutterFire so I'm going to close the issue. Thank you