flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

🐛 [firebase_messaging] Foreground notifications doesn't fire `onMessage` event listener without fetching token prior to receiving

Open Repox opened this issue 5 years ago • 24 comments

Bug report

Foreground notifications is only triggered if I access the FCM token from the instance. Without that, the onMessage event is never fired

Steps to reproduce

Steps to reproduce the behavior:

Add the following, taken from the example, to the initState of your Widget:

    FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print('Got a message whilst in the foreground!');
      print('Message data: ${message.data}');

      if (message.notification != null) {
        print('Message also contained a notification: ${message.notification}');
      }
    });

Subscribe the device to any topic.

Send notification to said topic.

Flutter console outputs the following:

D/FLTFireMsgReceiver( 7471): broadcast received for message

Console outputs nothing else.

Expected behavior

I expected to receive a notification in the onMessage listener and get the following output from my notification:

D/FLTFireMsgReceiver(13478): broadcast received for message
I/flutter (13478): Got a message whilst in the foreground!
I/flutter (13478): Message data: {post_id: 3, click_action: FLUTTER_NOTIFICATION_CLICK, title: My Post}
I/flutter (13478): Message also contained a notification: Instance of 'RemoteNotification'

Additional context

Adding the following before my listener, will make it work as expected:


    FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
    
    // Getting the token makes everything work as expected
    _firebaseMessaging
        .getToken()
        .then((String? token) {
      assert(token != null);
    });

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        ...
   }

This doesn't seem right as it isn't documented. It could be the correct solution, but then that should be documented somehow.


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
danstorm:myapp/ $ flutter doctor -v                                                                                                                                                                                                 [10:51:16]
[✓] Flutter (Channel stable, 2.0.6, on macOS 11.3 20E232 darwin-x64, locale da-DK)
    • Flutter version 2.0.6 at /Users/danstorm/flutter
    • Framework revision 1d9032c7e1 (3 days ago), 2021-04-29 17:37:58 -0700
    • Engine revision 05e680e202
    • Dart version 2.12.3

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/danstorm/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.5, Build version 12E262
    • CocoaPods version 1.10.1

[✓] 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)

[✓] IntelliJ IDEA Community Edition (version 2019.2.4)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • 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

[✓] VS Code (version 1.51.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available)
    • ONEPLUS A6003 (mobile) • 3f61fc83 • android-arm64  • Android 10 (API 29)
    • Chrome (web)           • chrome   • web-javascript • Google Chrome 90.0.4430.93

• No issues found!
danstorm:ios/ $ flutter doctor                                                                                                                                                                                                    [13:19:29]
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.6, on macOS 11.3 20E232 darwin-x64, locale da-DK)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] IntelliJ IDEA Community Edition (version 2019.2.4)
[✓] VS Code (version 1.51.1)
[✓] Connected device (2 available)

• No issues found!


Flutter dependencies

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

Click To Expand
danstorm:myapp/ $ flutter pub deps -- --style=compact                                                                                                                                                                           [14:30:26]
Dart SDK 2.12.3
Flutter SDK 2.0.6
myapp 1.0.5+6

dependencies:
- async_builder 1.3.0 [flutter rxdart]
- cupertino_icons 1.0.2
- device_info 2.0.0 [flutter device_info_platform_interface]
- firebase_analytics 8.0.2 [firebase_analytics_platform_interface firebase_analytics_web firebase_core flutter meta]
- firebase_core 1.1.0 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_crashlytics 2.0.2 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace]
- firebase_messaging 9.1.3 [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_icons 1.1.0 [flutter]
- flutter_local_notifications 5.0.0+3 [flutter flutter_local_notifications_platform_interface platform timezone]
- flutter_spinkit 5.0.0 [flutter]
- flutter_svg 0.22.0 [flutter meta path_drawing vector_math xml]
- http 0.13.2 [async http_parser meta path pedantic]
- intl 0.17.0 [clock path]
- shared_preferences 2.0.5 [meta flutter shared_preferences_platform_interface shared_preferences_linux shared_preferences_macos shared_preferences_web shared_preferences_windows]

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
- device_info_platform_interface 2.0.1 [flutter meta plugin_platform_interface]
- fake_async 1.2.0 [clock collection]
- ffi 1.0.0
- file 6.1.0 [meta path]
- firebase 9.0.1 [http http_parser js]
- firebase_analytics_platform_interface 2.0.0 [flutter meta]
- firebase_analytics_web 0.3.0 [firebase firebase_analytics_platform_interface flutter flutter_web_plugins meta]
- 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_crashlytics_platform_interface 3.0.2 [collection firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_platform_interface 2.1.3 [firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 1.0.6 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- 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]
- http_parser 4.0.0 [charcode collection source_span string_scanner typed_data]
- js 0.6.3
- matcher 0.12.10 [stack_trace]
- meta 1.3.0
- path 1.8.0
- path_drawing 0.5.0 [vector_math meta path_parsing flutter]
- path_parsing 0.2.0 [vector_math meta]
- 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.1 [path_provider_platform_interface meta path flutter ffi win32]
- pedantic 1.11.0
- petitparser 4.1.0 [meta]
- platform 3.0.0
- plugin_platform_interface 2.0.0 [meta]
- process 4.2.1 [file path platform]
- rxdart 0.26.0
- 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]
- 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]
- timezone 0.7.0 [path]
- typed_data 1.3.0 [collection]
- vector_math 2.1.0
- win32 2.0.5 [ffi]
- xdg_directories 0.2.0 [meta path process]
- xml 5.1.0 [collection meta petitparser]


Repox avatar May 03 '21 12:05 Repox

Same problem, only after adding getToken() function does it start working

Adrek avatar May 14 '21 16:05 Adrek

i have the same problem, i added getToken () but this is causing my app to crash. appearing on the log screen: FATAL EXCEPTION: pool-14-thread-1

dmrcierhn avatar May 15 '21 14:05 dmrcierhn

this was a solution for me, firebase_messaging: ^9.1.4 works as expected as says @Repox , thanks

alexlovar avatar May 15 '21 20:05 alexlovar

The update from 9.1.3 to 9.1.4 did not fix the issue for me, however i also found the same workaround that you mentioned above.

Future<void> init() async {
    await FirebaseMessaging.instance.getToken();
    FirebaseMessaging.onMessage.listen(receivedMessage);
}

void receivedMessage(RemoteMessage remoteMessage){
    ...
}

this works fine for me

blinddateapp1st avatar May 16 '21 18:05 blinddateapp1st

Same problem here. I use

  firebase_core: ^1.3.0
  firebase_messaging: ^10.0.2

The getToken() doesn't even have to be right before the listen. It just has to be called before the listener starts.

For my production app I'm still using the old version (firebase messaging 7.0). It's problems like these that make me mistrust the new version and hold me from actually updating.

sc00n avatar Jun 11 '21 22:06 sc00n

Same problem. Only an issue on Android - works fine on iOS. Fixed by the described solution of calling: await FirebaseMessaging.instance.getToken(); before FirebaseMessaging.onMessage.listen() firebase_core: ^1.4.0 firebase_messaging: ^10.0.4

[✓] Flutter (Channel stable, 2.2.3, on macOS 11.4 20F71 darwin-x64, locale en-GB) • Flutter version 2.2.3 at /Volumes/****/flutter • Framework revision f4abaa0735 (4 weeks ago), 2021-07-01 12:46:11 -0700 • Engine revision 241c87ad80 • Dart version 2.13.4

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/****/Library/Android/sdk • Platform android-30, build-tools 30.0.3 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.5, Build version 12E262 • CocoaPods version 1.10.1

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.2) • 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 11.0.8+10-b944.6916264)

[✓] VS Code (version 1.58.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.24.0

[✓] Connected device (2 available) • sdk gphone x86 arm (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator) • Chrome (web) • chrome • web-javascript • Google Chrome 92.0.4515.107

icecandy avatar Jul 30 '21 09:07 icecandy

Check if you are closing the stream subscription returned from listen.

guihcs avatar Jul 30 '21 19:07 guihcs

I wasn't closing the stream subscription and nothing documented about that in the flutter notifications documentation. But surely that is only relevant if you want to stop notifications. My controller is opened at start and stays open for the whole app cycle.

This happens on a fresh install and app startup. So no navigation away from my screen awaiting a notification or anything else happening.

icecandy avatar Aug 02 '21 11:08 icecandy

same issue here.

I fixed it by downgrading to firebase_messaging: ^10.0.1

HawkarShwany avatar Sep 30 '21 18:09 HawkarShwany

Same issue here. Any updates?

gonzalonm avatar Oct 21 '21 19:10 gonzalonm

Same issue. Fixed callin await FirebaseMessaging.instance.getToken() before FirebaseMessaging.onMessage.listen

andrebianchessi avatar Nov 26 '21 04:11 andrebianchessi

Same issue. Fixed callin await FirebaseMessaging.instance.getToken() before FirebaseMessaging.onMessage.listen

luisfuentech avatar Dec 02 '21 01:12 luisfuentech

I also had this issue which I fixed as above

amariucaitheodor avatar Dec 14 '21 17:12 amariucaitheodor

Same issue. Fixed callin await FirebaseMessaging.instance.getToken() before FirebaseMessaging.onMessage.listen

I had the same issue after upping version of Flutter and Firebase Messaging and this fix helped me !

albanlorillard avatar Dec 28 '21 10:12 albanlorillard

Do you have to await the FirebaseMessaging.instance.getToken() to make it work ?
It was working in my case without it but if it's a matter of milliseconds... I prefer not bet on luck :)

Tom3652 avatar Dec 30 '21 18:12 Tom3652

same issue

xang555 avatar Feb 25 '22 14:02 xang555

/cc @Salakar @russellwheatley

darshankawar avatar Apr 07 '22 11:04 darshankawar

I have the same issue. It also works calling some other method on the instance before calling FirebaseMessaging.onMessage.listen. Like FirebaseMessaging.instance.isAutoInitEnabled.

tobiasjunsten avatar Jun 20 '22 05:06 tobiasjunsten

I've just commented out the getToken() API and ran the example app. Received the foreground notification. @Repox is it an intermittent bug?

russellwheatley avatar Jun 20 '22 08:06 russellwheatley

Hey @Repox. 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 Jun 29 '22 01:06 google-oss-bot