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.46k stars 3.91k forks source link

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

Closed woprandi closed 3 years ago

woprandi commented 4 years ago

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

dhakadv06 commented 4 years ago

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

brian2694 commented 4 years ago

Facing same issue.!

hoahuathien commented 4 years ago

Facing the same problem with onMessage and onResume.

rodrigolmacedo commented 4 years ago

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

scognito commented 4 years ago

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

eyoeldefare commented 4 years ago

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 commented 4 years ago

Facing same issue.

woprandi commented 4 years ago

I confirm it's only on Android

woprandi commented 4 years ago

onResume is also called twice

sergitovar91 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

woprandi commented 4 years ago

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

janosdupai commented 4 years ago

@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 commented 4 years ago

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

janosdupai commented 4 years ago

@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 commented 4 years ago

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

scognito commented 4 years ago

@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 commented 4 years ago

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

eyoeldefare commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

A working workaround :

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

Thanks @woprandi for filing the issue and diagnosing this! Just as a warning, that workaround should fix this specific bug for firebase_messaging but may cause issues in other plugins like google_maps_flutter because of a bug with the current registration flow. We're tracking the larger issue at flutter/flutter#49365.

woprandi commented 4 years ago

@mklim Thanks for the info, I wasn't able to find the goal of the MainActivity content.

hp1227 commented 4 years ago

@mklim is there any updates?

mklim commented 4 years ago

@hp1227 yes. The fixes for the underlying issue have rolled into Flutter as of flutter/flutter@4fbb85e0c207132e0ed9130d7410784c00f0d066, but aren't on any of the channels besides master. I'm hoping to be able to hotfix this into stable, but don't have news to report on that front yet.

The root cause of this is flutter/flutter#49365. I think this issue is still worth keeping open in case firebase_messaging can work around the underlying Flutter problem individually, but that's probably the right bug to track for updates on the issue for now.

woprandi commented 4 years ago

Thanks for your work @mklim

davidwilcox commented 4 years ago

I just tried against master (looks like the commit is not yet merged to stable), and it worked for me!

rodrigolmacedo commented 4 years ago

I just tried against master (looks like the commit is not yet merged to stable), and it worked for me!

here too, but with backgroundmessage register bug.

viniciusroland commented 4 years ago

woprandi comment solves the issue for android, but how to solve the same issue for iOS?

noobcoder17 commented 4 years ago

A working workaround :

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

This works for me in Android.

kbrmimbyl commented 4 years ago

We're on Flutter 1.12.13+hotfix.7 and facing this issue right now. Is this bug fixed in 1.12.13+hotfix.8?

ghost commented 4 years ago

We're on Flutter 1.12.13+hotfix.7 and facing this issue right now. Is this bug fixed in 1.12.13+hotfix.8?

I am running 1.12.13+hotfix.8 on stable and it still does not work. Installing fvm to switch between master and stable.

fxsalazar commented 4 years ago

I'm running v1.12.13+hotfix.8 but with firebase_messaging: ^5.1.6 because when trying to update to v6.x the notifications on iOS stoped working. Not really sure where or what has the issue.

pdelgadohurtado commented 4 years ago

Still not working in flutter v1.12.13+hotfix.8 with firebase_messaging: ^6.0.12

nizarhmain commented 4 years ago

Tried it with flutter v1.15.19 dev. It seems to work for me https://storage.googleapis.com/flutter_infra/releases/dev/windows/flutter_windows_v1.15.19-dev.zip

ghost commented 4 years ago

Yes, the master branch seems fine

schankam commented 4 years ago

When will this be merged to Stable ? Any updates ? As of today, the problem still persist.

woutergoossens commented 4 years ago

When will this be in the stable version?

p30arena commented 4 years ago

firebase_messaging 6.0.13 Flutter (Channel master, v1.16.4-pre.52, on Microsoft Windows [Version 6.3.9600], locale en-US)

lucas-sesti commented 4 years ago

@p30arena the version 6.0.13 is correct ? i'm using and still calling twice in the onResume and others...

p30arena commented 4 years ago

@lucas-sesti

maybe you should check your server logic, I've tested it in two production apps on Android and iOS.

lucas-sesti commented 4 years ago

But in not stable channel of flutter alright? I'm using the Stable channel, now i see you said: "Flutter (Channel master, v1.16.4-pre.52, on Microsoft Windows [Version 6.3.9600], locale en-US)"...

lexyfeito commented 4 years ago

Hi, any ideas when this will be merge to stable?

tzvc commented 4 years ago

Any ETA on the merge to stable? :)

Danaru87 commented 4 years ago

Not resolved with flutter 1.12.13+hotfix.9.

Any news about merging into stable version ?

vinkalathmin commented 4 years ago

This is very serious issue. Is there there any work around, we have an urgent release. Please help

braindamagedman commented 4 years ago

Hello guys. I think, after flutter upgrade to 1.12, you need to migrate your projects following this guide: https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects#full-flutter-app-migration

body of the MainActivity is now empty

The new FlutterActivity no longer requires manually registering your plugins. It will now perform the registration automatically when the underlaying FlutterEngine is created.

huulbaek commented 4 years ago

I'm doing something like this:

if ((DateTime.now().microsecondsSinceEpoch - _lastOnResumeCall) > 1000000) {
  _lastOnResumeCall = DateTime.now().microsecondsSinceEpoch;
  _navigateToItemDetail(message);
}

Essentially testing how long since the callback was last reached. If some amount of time has passed we proceed.

mtannerfors commented 4 years ago

Fixed and working well on flutter beta. Thanks @mklim!