flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

πŸ› [firebase_messaging] `onBackgroundMessage` throws NoSuchMethodError on Android.

Open Kurogoma4D opened this issue 5 years ago β€’ 12 comments

Bug report

Describe the bug Simply, use firebase_messaging package and set onBackgroundMessage callback, then the app throws some exception below:

NoSuchMethodError: No top-level getter '_registerPlugins' declared.
Receiver: top-level
Tried calling: _registerPlugins

In my environment, the stack trace was missing so that no further information could be obtained. image

Steps to reproduce

Check my example project that reproduces this issue for details. ( A google-services.json is not committed, so please use your own Firebase project when try to run this. )

  1. Use Flutter version 2.0.4 (currently latest version of stable channel).
  2. Import firebase_messaging: ^9.1.1
  3. Set callback function for background message.
  4. Turn on Break on exceptions feature in Android Studio, and run app.

Expected behavior

Just run app with no errors.


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[βœ“] Flutter (Channel stable, 2.0.4, on macOS 11.1 20C69 darwin-x64, locale ja-JP)
[βœ“] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[βœ“] Xcode - develop for iOS and macOS
[βœ“] Chrome - develop for the web
[βœ“] Android Studio (version 4.1)
[βœ“] IntelliJ IDEA Ultimate Edition (version 2020.3.2)
[βœ“] IntelliJ IDEA Community Edition (version 2020.3.2)
[βœ“] VS Code (version 1.55.0)
[βœ“] Connected device (3 available)

β€’ No issues found!

Flutter dependencies

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

Click To Expand
Dart SDK 2.12.2
Flutter SDK 2.0.4
firebase_playground 1.0.0+1

dependencies:
- 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]

dev dependencies:
- 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]

transitive dependencies:
- async 2.5.0 [collection]
- boolean_selector 2.1.0 [source_span string_scanner]
- characters 1.1.0
- charcode 1.2.0
- clock 1.1.0
- collection 1.15.0
- fake_async 1.2.0 [clock collection]
- 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]
- flutter_web_plugins 0.0.0 [flutter js characters collection meta typed_data vector_math]
- js 0.6.3
- matcher 0.12.10 [stack_trace]
- meta 1.3.0
- path 1.8.0
- plugin_platform_interface 2.0.0 [meta]
- sky_engine 0.0.99
- source_span 1.8.0 [charcode collection path term_glyph]
- stack_trace 1.10.0 [path]
- stream_channel 2.1.0 [async]
- 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]
- typed_data 1.3.0 [collection]
- vector_math 2.1.0

Kurogoma4D avatar Apr 08 '21 06:04 Kurogoma4D

@Kurogoma4D The error:

NoSuchMethodError: No top-level getter '_registerPlugins' declared. Receiver: top-level Tried calling: _registerPlugins

indicates that it cannot find the value _registerPlugins in global scope because it may not have been imported or could be due to a parse error before the import.

Can you try running flutter analyze and see if it reports anything related to _registerPlugins ?

Also, can you also try the official flutter_messaging plugin example and see if you get similar error with it too or not ?

darshankawar avatar Apr 08 '21 08:04 darshankawar

@darshankawar

flutter analyze result is here:

$ flutter analyze
Analyzing firebase_playground...    
No issues found! (ran in 2.5s)

And I tried running firebase_messaging v9.1.1 example project, I got same error:

image

Kurogoma4D avatar Apr 08 '21 09:04 Kurogoma4D

Thanks for checking. I think this is coming from after calling

// Set the background messaging handler early on, as a named top-level function FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

Which in turn calls:

await channel.invokeMapMethod('Messaging#startBackgroundIsolate', { 'pluginCallbackHandle': bgHandle.toRawHandle(), 'userCallbackHandle': userHandle.toRawHandle(), });

And finally here:

Future<T?> _invokeMethod<T>(String method, { required bool missingOk, dynamic arguments }) async {
    assert(method != null);
    final ByteData? result = await binaryMessenger.send(
      name,
      codec.encodeMethodCall(MethodCall(method, arguments)),
    );
    if (result == null) {
      if (missingOk) {
        return null;
      }
      throw MissingPluginException('No implementation found for method $method on channel $name');
    }
    return codec.decodeEnvelope(result) as T?;
  }

darshankawar avatar Apr 08 '21 10:04 darshankawar

Hey @Kurogoma4D, I followed your instructions and I ran your code sample from both android studio and vscode. In both instances, I didn't receive an error. I did receive this log:

I/FLTFireMsgService( 4705): FlutterFirebaseMessagingBackgroundService started!

Perhaps there is an environment difference?

russellwheatley avatar Apr 15 '21 14:04 russellwheatley

Hi @russellwheatley , I tried reproduce this in my another environment, and confirmed it on Android Studio (VSCode couldn't catch that exception). In Android Studio, make sure set to break on all exceptions.

γ‚Ήγ‚―γƒͺγƒΌγƒ³γ‚·γƒ§γƒƒγƒˆ 2021-04-16 1 25 13

Kurogoma4D avatar Apr 15 '21 16:04 Kurogoma4D

Looks the same to me: Screenshot 2021-04-16 at 08 48 28

russellwheatley avatar Apr 16 '21 07:04 russellwheatley

Hi. I have same issue in firebase_messaging 9.1.2. Do you know how to fix it?

IlyaBlinov avatar Apr 20 '21 13:04 IlyaBlinov

Hey @Kurogoma4D. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Apr 29 '21 01:04 google-oss-bot

Update: As we continued to investigate this issue, I was able to confirm that only reproduces in Android Emulator with API Level 30.

Kurogoma4D avatar Apr 29 '21 04:04 Kurogoma4D

Hey @Kurogoma4D, did you manage to resolve this issue?

russellwheatley avatar Mar 08 '22 15:03 russellwheatley

Hi @russellwheatley, I was no longer aware of this issue because it doesn't affect my project now. I ran firebase_messaging example v9.1.4 with Flutter v2.2.3, and confirmed that there is no error.

Thanks.

Kurogoma4D avatar Mar 09 '22 02:03 Kurogoma4D

Thanks for the feedback, @Kurogoma4D.

russellwheatley avatar Mar 09 '22 09:03 russellwheatley