Skip to content
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] FirebaseMessaging.onMessageOpenedApp.listen((message){} triggering more than once #7243

Closed
pranav-asthi opened this issue Oct 26, 2021 · 17 comments
Assignees
Labels
plugin: messaging resolution: user This was a user issue, e.g. invalid configuration or code. type: bug Something isn't working

Comments

@pranav-asthi
Copy link

pranav-asthi commented Oct 26, 2021

FirebaseMessaging.onMessageOpenedApp.listen((message){} triggering more than once for a single notification

@pranav-asthi pranav-asthi added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Oct 26, 2021
@darshankawar darshankawar added the triage Issue is currently being triaged. label Oct 26, 2021
@darshankawar
Copy link

@pranav-asthi
Can you provide flutter doctor -v, a minimal code sample along with pubspec.yaml ?
Also check if this issue is similar to your case #4188

@darshankawar darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Oct 26, 2021
@pranav-asthi
Copy link
Author

pranav-asthi commented Oct 26, 2021

[√] Flutter (Channel stable, 2.5.1, on Microsoft Windows [Version 10.0.19042.1237], locale en-US)
    • Flutter version 2.5.1 at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ffb2ecea52 (6 weeks ago), 2021-09-17 15:26:33 -0400
    • Engine revision b3af521a05
    • Dart version 2.14.2

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\prana\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • 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-b01)

[√] VS Code (version 1.61.2)
    • VS Code at C:\Users\prana\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.21.0

[√] Connected device (2 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 95.0.4638.54
    • Edge (web)   • edge   • web-javascript • Microsoft Edge 95.0.1020.30

! Doctor found issues in 1 category.

pubspec.yaml

  google_maps_flutter: ^2.0.8
  intl: ^0.17.0
  package_info: ^2.0.2
  webview_flutter: ^2.0.13
  modal_progress_hud_nsn: ^0.1.0-nullsafety-1
  xml: ^5.3.0
  flutter_image_compress: ^1.1.0
  firebase_core: ^1.7.0
  firebase_messaging: ^10.0.9
  flutter_local_notifications: ^9.0.0

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Oct 26, 2021
@pranav-asthi
Copy link
Author

pranav-asthi commented Oct 26, 2021

FirebaseMessaging.instance.getInitialMessage().then((_message){
     if(_message!=null)
       {
        // print("Background Notification");
         final route=_message.data["route"];
         navigateTo(route);
       }
   });
   FirebaseMessaging.onMessageOpenedApp.listen((message) {
     if(message.notification!=null && !isNotified)// isNotified doesn't matter
       {
        // print("Terminated Notification $isNotified:${message.notification!.title}");
         final route=message.data["route"];
         navigateTo(route);
        // isNotified=true;
       }
   });
   FirebaseMessaging.onMessage.listen((RemoteMessage message) {
     if(message.notification!=null)
       {
        // print("Foreground Notification :${message.notification!.title}");
         FCM.init(message);
       }
   });

@pranav-asthi
Copy link
Author

@pranav-asthi Can you provide flutter doctor -v, a minimal code sample along with pubspec.yaml ? Also check if this issue is similar to your case #4188

The issue is some what similar to that of 4188 but the handler is different here

@darshankawar
Copy link

@pranav-asthi
What state is your app in when onMessageOpenedApp.listen is triggering more than once ? Is it in background or in foreground ?
From your code snippet, is below getting called properly ?

FirebaseMessaging.instance.getInitialMessage().then((_message){
if(_message!=null)
{
// print("Background Notification");
final route=_message.data["route"];
navigateTo(route);
}
});

When you trigger onMessageOpenedApp, it also need to listen to RemoteMessage, does it get called ?

Also, just to confirm, can you try the official plugin example and see if you get same behavior ?

@darshankawar darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Oct 27, 2021
@pranav-asthi
Copy link
Author

It happens only when the app is in background state. Yes ,the getInitialMessage() working properly .

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Oct 27, 2021
@pranav-asthi
Copy link
Author

pranav-asthi commented Oct 27, 2021

FirebaseMessaging.onMessageOpenedApp.listen((message) {
     if(message.notification!=null && !isNotified)// isNotified doesn't matter
       {
        // print("Terminated Notification $isNotified:${message.notification!.title}");
         final route=message.data["route"];
         navigateTo(route);
        // isNotified=true;
       }
   });

In this handler, the navigation and printing happens twice

@darshankawar
Copy link

Thanks for the update. Seeing the same behavior using latest plugin version on stable and master.

stable, master flutter doctor -v
[✓] Flutter (Channel stable, 2.5.3, on Mac OS X 10.15.4 19E2269 darwin-x64,
    locale en-GB)
    • Flutter version 2.5.3 at /Users/dhs/documents/fluttersdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 18116933e7 (2 days ago), 2021-10-15 10:46:35 -0700
    • Engine revision d3ea636dc5
    • Dart version 2.14.4

[✓] Android toolchain - develop for Android devices (Android SDK version 30)
    • Android SDK at /Users/dhs/Library/Android/sdk
    • Platform android-30, build-tools 30.0.3
    • ANDROID_HOME = /Users/dhs/Library/Android/sdk
    • Java binary at: /Users/dhs/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/202.7486908/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.3, Build version 12C33
    • 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 /Users/dhs/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/202.7486908/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.57.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.21.0

[✓] Connected device (4 available)
    • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
      ios            • iOS 14.4.1 18D61
    • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
      ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
    • macOS (desktop)            • macos                                    •
      darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
    • Chrome (web)               • chrome                                   •
      web-javascript • Google Chrome 93.0.4577.82


• No issues found!

[✓] Flutter (Channel master, 2.6.0-12.0.pre.521, on Mac OS X 10.15.4 19E2269
    darwin-x64, locale en-GB)
    • Flutter version 2.6.0-12.0.pre.521 at
      /Users/dhs/documents/fluttersdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 6602107bb2 (83 minutes ago), 2021-10-28 20:20:11 -0700
    • Engine revision ddf4bd598e
    • Dart version 2.15.0 (build 2.15.0-262.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30)
    • Android SDK at /Users/dhs/Library/Android/sdk
    • Platform android-30, build-tools 30.0.3
    • ANDROID_HOME = /Users/dhs/Library/Android/sdk
    • Java binary at: /Users/dhs/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/202.7486908/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.1, Build version 12E507
    • 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 /Users/dhs/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/202.7486908/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.57.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.21.0

[[✓] Connected device (4 available)
    • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
      ios            • iOS 14.4.1 18D61
    • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
      ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
    • macOS (desktop)            • macos                                    •
      darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
    • Chrome (web)               • chrome                                   •
      web-javascript • Google Chrome 93.0.4577.82


• No issues found!




@darshankawar
Copy link

similar / related : #4188

@darshankawar darshankawar added plugin: messaging and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. labels Oct 29, 2021
@Vov4yk
Copy link

Vov4yk commented Nov 10, 2021

Faced the same issue.
Thing is there should be overlapping of getInitialMessage and onMessageOpenedApp stream due documentation. But it happens when app is opened from terminated state.
From messaging.dart:
`
/// Returns a [Stream] that is called when a user presses a notification message displayed
/// via FCM.
///
/// A Stream event will be sent if the app has opened from a background state
/// (not terminated).
///
/// If your app is opened via a notification whilst the app is terminated,
/// see [getInitialMessage].

static Stream get onMessageOpenedApp =>
FirebaseMessagingPlatform.onMessageOpenedApp.stream;

`

@darshankawar darshankawar changed the title 🐛 [firebase_messaging] FirebaseMessaging.onMessageOpenedApp.listen((message){} 🐛 [firebase_messaging] FirebaseMessaging.onMessageOpenedApp.listen((message){} triggering more than once Nov 17, 2021
@russellwheatley russellwheatley self-assigned this Nov 19, 2021
@vincepunkrock
Copy link

I had the same problem, also with FirebaseMessaging.onMessage.listen.

I fixed it by putting the StreamSubscription return by the listen function in a variable. Then, before I call FirebaseMessaging.onMessage.listen, I check if the subscription already has been set, if yes, I don't make the call.

if(_onMessageSub == null) {
_onMessageSub = FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("Notification received when app is in the foreground");
_handlePushNotificationPayload(message);
});
}

Otherwise, it seems that the call to .listen creates multiple subscriptions and in my case it was triggered up to 6 times.
I'm not sure if it's the right way to go about it, but it seems to solve the problem. Maybe the example on pub.dev should be updated?

@abigotado
Copy link

I had the same problem, also with FirebaseMessaging.onMessage.listen.

I fixed it by putting the StreamSubscription return by the listen function in a variable. Then, before I call FirebaseMessaging.onMessage.listen, I check if the subscription already has been set, if yes, I don't make the call.

if(_onMessageSub == null) { _onMessageSub = FirebaseMessaging.onMessage.listen((RemoteMessage message) { print("Notification received when app is in the foreground"); _handlePushNotificationPayload(message); }); }

Otherwise, it seems that the call to .listen creates multiple subscriptions and in my case it was triggered up to 6 times. I'm not sure if it's the right way to go about it, but it seems to solve the problem. Maybe the example on pub.dev should be updated?

@vincepunkrock Could you please share a bit more code? Does the subscription prevent multiple calls?

@Bhagat8533
Copy link

Bhagat8533 commented Jul 25, 2022

@abigotado Here how I have fixed this issue for me

 if(messageListenerStream==null)
    {
      messageListenerStream = FirebaseMessaging.onMessage.listen((RemoteMessage message) {
             handlePayload(message);
           });
    }
    else {
      messageListenerStream!.onData((RemoteMessage? message) {
         handlePayload(message);
      });
    }

@abigotado
Copy link

@Bhagat8533 Thank you very much!

@russellwheatley
Copy link
Member

Hey @pranav-asthi, which platform are you experiencing this on?

@pranav-asthi
Copy link
Author

i fixed it a long time ago and i forgot to close this issue

@darshankawar darshankawar added the resolution: user This was a user issue, e.g. invalid configuration or code. label Aug 30, 2022
@firebase firebase locked and limited conversation to collaborators Sep 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
plugin: messaging resolution: user This was a user issue, e.g. invalid configuration or code. type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants