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] onMessage/onResume is called twice on Android/iOS #1669

Closed
woprandi opened this issue Dec 17, 2019 · 87 comments · Fixed by flutter/engine#15979 or #4012
Closed

[firebase_messaging] onMessage/onResume is called twice on Android/iOS #1669

woprandi opened this issue Dec 17, 2019 · 87 comments · Fixed by flutter/engine#15979 or #4012
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) platform: android Issues / PRs which are specifically for Android. platform: ios Issues / PRs which are specifically for iOS. plugin: messaging type: bug Something isn't working

Comments

@woprandi
Copy link

Describe the bug
The onMessage callback is always called twice

To Reproduce
Steps to reproduce the behavior:

  1. Setup the plugin, do a print statement in the onMessage callback
  2. Keep the app in the foreground
  3. Send a FCM message
  4. 2 print on the console

Expected behavior
Only one print on the console

Additional context
I don' t have the same issue with onBackgroundMessage. It's called once as expected

@woprandi woprandi added the type: bug Something isn't working label Dec 17, 2019
@dhakadv06
Copy link

Facing same issue. onResume and onLaunch works fine but onMessage triggers twice.

@brian2694
Copy link

Facing same issue.!

@hoahuathien
Copy link

hoahuathien commented Jan 6, 2020

Facing the same problem with onMessage and onResume.

@rodrigolmacedo
Copy link

rodrigolmacedo commented Jan 6, 2020

Same issue here, its is boring, in other app with other version the bug dont occurs T_T

@scognito
Copy link

I confirm (firebase messaging 6.0.9, and flutter 1.12.13+hotfix.5)
but it happens only on Android, not iOS.

@eyoeldefare
Copy link

Only on the android, it seems to me. Its easy to handle programmatically but not sure if there's a clean way to deal with it.

@CoderJava
Copy link

Facing same issue.

@iapicca iapicca added the impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) label Jan 21, 2020
@woprandi
Copy link
Author

I confirm it's only on Android

@woprandi
Copy link
Author

onResume is also called twice

@woprandi woprandi changed the title [firebase_messaging] onMessage is called twice [firebase_messaging] onMessage/onResume is called twice on Android Jan 21, 2020
@sergitovar91
Copy link

sergitovar91 commented Jan 21, 2020

Here the same issue, I get duplicate notifications on android (onResume and OnMessage). Here you will find all my details. It is the default flutter project with firebase firebase_messaging: ^6.0.9

Output:
W/FirebaseMessaging( 7129): Unable to log event: analytics library is missing
W/FirebaseMessaging( 7129): Unable to log event: analytics library is missing
I/flutter ( 7129): onMessage: {notification: {title: prueba, body: prueba}, data: {click_action: FLUTTER_NOTIFICATION_CLICK}}
I/flutter ( 7129): onMessage: {notification: {title: prueba, body: prueba}, data: {click_action: FLUTTER_NOTIFICATION_CLICK}}

flutter.txt
code.txt

@janosdupai
Copy link

same issue on iOS. onMessage called twice with different message ID, so there could be the issue on server side.
I double checked that my php call run only once, but somehow I received 2 message. In both cases: foreground and background too....

@EverestBoy
Copy link

Same here, on Message is called twice, flutter team are you listening?

@woprandi
Copy link
Author

@janosdupai I don't have this issue on iOS. I tested again today

@janosdupai
Copy link

@woprandi
I use Firebase Cloud Message for a Flutter app.

I try to trigger a push notification with php code. It give me a success: 1 result, but on my device I always receive 2 message with the same title&body but different message ID (which is provided by google firebase).

I double checked, that I trigger this function only once on a server side.

my code on the server


    $url = 'https://fcm.googleapis.com/fcm/send';
     $msg = array
          (
                'body'  => $message,
                'title' => $title,
                        'badge' => 1,/*Default sound*/
                        'sound' => 'default',
          );
    $fields = array
            (
                'registration_ids' => $id,
                'notification'  => $msg
            );

    $fields = json_encode ( $fields );

    $apiKey = 'XXXXXXX'; //IOS

    $headers = array (
            'Authorization: key='.$apiKey,
            'Content-Type: application/json'
    );

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    curl_close ( $ch );
        echo $result;

I tried to trigger this from Terminal, which worked fine, and sent only 1 message. My terminal code:

DATA='{"notification": {"body": "this is a body","title": "this is a title"}, "priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done"}, "to": "MYDEVICETOKEN"}'
curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d "$DATA" -H "Authorization: key=XXXXXXXXX"

@woprandi
Copy link
Author

@janosdupai Can you check the FCM response via curl -i ?

@janosdupai
Copy link

janosdupai commented Jan 22, 2020

@woprandi

{"multicast_id":8502274311835153380,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"1579683773320267"}]}

I receive this message_id in flutter app, but I receive another one in a small delay (with same message title and body)....

on flutter console

flutter: {status: done, id: 1, click_action: FLUTTER_NOTIFICATION_CLICK, google.c.a.e: 1, gcm.message_id: 1579683773320267, aps: {alert: {title: This is title #1, body: here is a message. message}, sound: default}}
flutter: {status: done, id: 1, click_action: FLUTTER_NOTIFICATION_CLICK, google.c.a.e: 1, gcm.message_id: 1579685542411682, aps: {alert: {title: This is title #1, body: here is a message. message}, sound: default}}

@CoderJava
Copy link

CoderJava commented Jan 22, 2020

I'm test used terminal but the result it's same. onMessage is called twice. I'm testing on Android device.

@scognito
Copy link

@woprandi

{"multicast_id":8502274311835153380,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"1579683773320267"}]}

I receive this message_id in flutter app, but I receive another one in a small delay (with same message title and body)....

on flutter console

flutter: {status: done, id: 1, click_action: FLUTTER_NOTIFICATION_CLICK, google.c.a.e: 1, gcm.message_id: 1579683773320267, aps: {alert: {title: This is title #1, body: here is a message. message}, sound: default}}
flutter: {status: done, id: 1, click_action: FLUTTER_NOTIFICATION_CLICK, google.c.a.e: 1, gcm.message_id: 1579685542411682, aps: {alert: {title: This is title #1, body: here is a message. message}, sound: default}}

I receive it two times even from terminal

@sudip92
Copy link

sudip92 commented Jan 22, 2020

Facing same issue with firebase_messaging: ^6.0.9. onMessage and onResume called multiple times

@eyoeldefare
Copy link

Same here, on Message is called twice, flutter team are you listening?

Can you imagine how many issues there are under this package? You need to be more appreciative of their craft bro. They can't solve everything overnight, give them some time.

@woprandi
Copy link
Author

After fast investigation, the method onAttachedToEngine of the class FirebaseMessagingPlugin, which call registerReceiver, is called twice. That explain the double call.
I will try to continue to investigate but I'm not an expert

@woprandi
Copy link
Author

A working workaround :

class MainActivity: FlutterActivity() {
   /*  override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }*/
}

@cayau-wamsa
Copy link

For me, works on update Flutter version 1.17.5 and firebase_messaging: ^6.0.16

@fnicastri
Copy link

I'm facing the same issue on iOS

@rivetingpeppermint
Copy link

Still facing the same issue on both Android and iOS. I'm using firebase_messaging: ^6.0.16 and Flutter 1.17.5.

@Chethansmash7
Copy link

I think I solved the issue.But I need to be sure as to what steps you are following.Those who have the issue please reply to this comment with the steps by step procedure...

@fnicastri
Copy link

@Chethansmash7

  1. Clone this repo
  2. change app id and put the new GoogleService-Info.plist in the project
  3. build packages/firebase_messaging/example
  4. send a notification while the app is in foreground
  5. onMessage get called twice
flutter: onMessage: {body: Body, google.c.sender.id: 276852428296, google.c.a.e: 1, click_action: FLUTTER_NOTIFICATION_CLICK, aps: {alert: {title: Title, body: Test notification}, content-available: 1}, title: title, gcm.message_id: 1594807744573443, priority: high, content_available: true, gcm.notification.priority: high}

flutter: onMessage: {click_action: FLUTTER_NOTIFICATION_CLICK, google.c.sender.id: 276852428296, google.c.a.e: 1, content_available: true, aps: {alert: {title: Title, body: Test notification}, content-available: 1}, title: title, gcm.message_id: 1594807744573443, body: Body, priority: high, gcm.notification.priority: high}

Flutter doctor

[✓] Flutter (Channel stable, v1.17.5, on Mac OS X 10.15.5 19F101, locale en-US)
    • Flutter version 1.17.5 at /Users/cico/src/flutter
    • Framework revision 8af6b2f038 (2 weeks ago), 2020-06-30 12:53:55 -0700
    • Engine revision ee76268252
    • Dart version 2.8.4

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
    • Android SDK at /Users/cico/Library/Android/sdk
    • Platform android-30, build-tools 30.0.0
    • ANDROID_SDK_ROOT = /Users/cico/Library/Android/sdk
    • 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-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.5, Build version 11E608c
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 47.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

Can you share your solution?

@fnicastri
Copy link

I've noticed that the callback onMessage got called twice in FLTFirebaseMessagingPlugin.m

commenting out

if #available(iOS 10.0, *) {
  UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}

did the trick.
But obviously it is just a work around, I'm not aware of the possible repercussions.

The flow is:

  1. (BOOL)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
    get called and
  2. it calls void)didReceiveRemoteNotification:(NSDictionary *)userInfo
  3. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler NS_AVAILABLE_IOS(10.0) get called
  4. and it call a second time [_channel invokeMethod:@"onMessage" arguments:userInfo]

@rivetingpeppermint
Copy link

I tried @fnicastri's workaround and it works for onMessage, but onResume is still called twice on iOS. onResume is called when the app is still in the background as well as when the app is brought to the foreground. Is it supposed to be this way? From reading the documentation, I thought onResume will only be called when the app is brought to the foreground, and not when it's in the background too.

@fnicastri
Copy link

@rivetingpeppermint @TahaTesser
Yes, digging more I noticed the same issue.
This is a serious issue that makes notifications unusable.

@rivetingpeppermint
Copy link

rivetingpeppermint commented Jul 16, 2020

I also just found out that when I run my app on iOS, onResume and onLaunch are called when we open the app normally (from the home screen, not from clicking the notification in the system tray). This behavior doesn't happen with Android. I used the iPad Pro (9.7-inch, running iOS 13.5.1) and iPhone 7 (running iOS 12.4.1).

@TahaTesser TahaTesser reopened this Jul 16, 2020
@fnicastri
Copy link

@TahaTesser
Thanks for reopening this bug.
you could change the platform tag too and maybe the title.

@TahaTesser TahaTesser added the platform: ios Issues / PRs which are specifically for iOS. label Jul 17, 2020
@TahaTesser TahaTesser changed the title [firebase_messaging] onMessage/onResume is called twice on Android [firebase_messaging] onMessage/onResume is called twice on Android/iOS Jul 17, 2020
@hasanzia1993
Copy link

I confirm it's only on Android

it on iOS too

@mklim mklim removed their assignment Aug 11, 2020
@HUYTHAINGUYEN
Copy link

Does anyone have a workaround for this problem yet?

@eyoeldefare
Copy link

Does anyone have a workaround for this problem yet?

init firebase only once, it worked for me

@fnicastri
Copy link

Does anyone have a workaround for this problem yet?

init firebase only once, it worked for me

Can you explain it?

@ollyde
Copy link

ollyde commented Aug 19, 2020

It's fixed with the new version. I'm using this current configuration: (Don't use carets it's not good)

  firebase_core: 0.5.0
  firebase_auth: 0.18.0
  firebase_messaging: 7.0.0

Be aware there is now some additional Android setup 👎

@rivetingpeppermint
Copy link

@OllyDixon what additional setup?

@ollyde
Copy link

ollyde commented Aug 25, 2020

@rivetingpeppermint ahh sorry, it's on the github/pub page.

@fnicastri
Copy link

fnicastri commented Aug 29, 2020

@OllyDixon @TahaTesser

It's fixed with the new version. I'm using this current configuration: (Don't use carets it's not good)

  firebase_core: 0.5.0
  firebase_auth: 0.18.0
  firebase_messaging: 7.0.0

Be aware there is now some additional Android setup 👎

It's not.
I still get onResume called twice on iOS. On Android it's fine now (no additional setup needed)
And if I add

if #available(iOS 10.0, *) {
  UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}

I get nothing at all.

Using the example I get onMessage twice and onResume once.
Something is not working at all even with this new release.

@dppatel03
Copy link

firebase_core: 0.5.0
firebase_auth: 0.18.0
firebase_messaging: 7.0.0

I am getting same issue at iOS.

@kartercs
Copy link

kartercs commented Oct 8, 2020

Having the same issue on IOS for onMessage and onResume.
If it can help the second message object have the same informations as the first one but in different order like this :

{sender: ***, google.c.sender.id: ***, google.c.a.e: ***, click_action: FLUTTER_NOTIFICATION_CLICK, aps: {alert: {title: ***, body: ***}, sound: ***, content-available: ***}, sound: ***, content_available: ***, priority: ***, gcm.message_id: ***, gcm.notification.priority: ***}

{click_action: FLUTTER_NOTIFICATION_CLICK, sender: ***, google.c.a.e: ***, google.c.sender.id: ***, aps: {alert: {title: ***e, body: ***}, sound: default, content-available: 1}, sound: ***, gcm.message_id: ***, priority: ***, content_available: ***, gcm.notification.priority: ***}

@salva73
Copy link

salva73 commented Nov 11, 2020

Same issue here, using flutter v. 1.22.0 and firebase messaging 7.0.3, this behavior happens when I use "content-available": true

@TahaTesser
Copy link

@salva73
Could you please file a new issue with all the steps

@firebase firebase locked and limited conversation to collaborators Dec 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) platform: android Issues / PRs which are specifically for Android. platform: ios Issues / PRs which are specifically for iOS. plugin: messaging type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.