Skip to content

[firebase_messaging] Both onMessage and onLaunch called with same gcm.message_id #1911

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

Closed
RabbitKabong opened this issue Jan 31, 2020 · 4 comments
Labels
impact: customer A bug with low impact (e.g. affecting only a few customers or has a workaround). (P3) plugin: messaging Stale Issue with no recent activity type: bug Something isn't working

Comments

@RabbitKabong
Copy link

Describe the bug
After sending a notification via google-api-php-client to GCM, I receive the same message twice on a device: both onMessage and onLaunch are triggered with the same gcm.message_id.

To Reproduce
Steps to reproduce the behavior:

  1. Send message to an individual iPhone 11 device (with its token or using a topic) via google-api-php-client:
["message"]=>
  array(6) {
    ["token"]=>
    string(152) "<TOKEN>"
    ["notification"]=>
    array(2) {
      ["title"]=>
      string(12) "<TITLE>"
      ["body"]=>
      string(48) "<BODY>"
    }
    ["data"]=>
    array(5) {
      ["category"]=>
      string(7) "<CATEGORY>"
      ["type"]=>
      string(16) "<TYPE>"
      ["title"]=>
      string(33) "<TITLE>"
      ["url"]=>
      string(62) "<URL>"
      ["id"]=>
      string(4) "<ID>"
    }
    ["apns"]=>
    array(1) {
      ["payload"]=>
      array(1) {
        ["aps"]=>
        array(2) {
          ["sound"]=>
          string(7) "default"
          ["content-available"]=>
          int(1)
        }
      }
    }
    ["android"]=>
    array(2) {
      ["priority"]=>
      string(4) "HIGH"
      ["notification"]=>
      array(2) {
        ["click_action"]=>
        string(26) "FLUTTER_NOTIFICATION_CLICK"
        ["sound"]=>
        string(7) "default"
      }
    }
    ["fcm_options"]=>
    array(1) {
      ["analytics_label"]=>
      string(16) "<LABEL>"
    }
  }
  1. With my app closed, when I tap on the notification in iOS, the same message is handled by the device twice. Here's what's comes in the log in Console:

onMessage

flutter: FCM: onMessage: {category: <CATEGORY>, google.c.a.e: 1, id: <ID>, aps: {alert: {title: <TITLE>, body: <BODY>}, sound: default, content-available: 1.0}, title: <TITLE>, type: <TYPE>, google.c.a.m_l: <CATEGORY>, gcm.message_id: 1580433692277472, url: <URL>}

onLaunch

flutter: FCM: onLaunch: {category: <CATEGORY>, google.c.a.e: 1, id: <ID>, aps: {alert: {title: <TITLE>, body: <BODY>}, sound: default, content-available: 1.0}, title: <TITLE>, type: <TYPE>, google.c.a.m_l: <CATEGORY>, gcm.message_id: 1580433692277472, url: <URL>}

Expected behavior
With the app closed, I'd expect to see onLaunch processed by the app, and not onMessage.

Additional context
I'm using firebase_core ^0.4.3+1, firebase_messaging ^6.0.9, and firebase_analytics ^5.0.9 in my app at the moment.

I haven't been able to reproduce this issue in the iOS Simulator, Android simulator, or on an Android device. I haven't been able to reproduce this on several other iOS devices I have access to, either. Unsubscribing from notifications (via unsubscribeFromTopic()), reinstalling the app, and then resubscribing didn't make a difference.

Not sure if it matters, but the two devices I can replicate this on were both recently upgraded and migrated from older iPhones.

Here's the code snippets I use to handle the notifications:

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> m) async {
        debugPrint('FCM: onMessage: $m');
        m['messageType'] = 'onMessage';
        _handleFCMMessage(m);
      },
      onLaunch: (Map<String, dynamic> m) async {
        debugPrint('FCM: onLaunch: $m');
        m['messageType'] = 'onLaunch';
        _handleFCMMessage(m);
      },
      onResume: (Map<String, dynamic> m) async {
        debugPrint('FCM: onResume: $m');
        m['messageType'] = 'onResume';
        _handleFCMMessage(m);
      },
    );
@RabbitKabong RabbitKabong added the type: bug Something isn't working label Jan 31, 2020
@iapicca
Copy link

iapicca commented Feb 3, 2020

Hi @RabbitKabong
I see there's an open issue addressing the case you described.
Please follow up on that issue,
I'm closing the current one as duplicate.
If you disagree please write in the comments
and I will reopen it.
Thank you

@iapicca iapicca closed this as completed Feb 3, 2020
@RabbitKabong
Copy link
Author

Yeah, when I did some initial investigation I saw #1669, too, but 2 things stick out to me:

  1. The examples given in [firebase_messaging] onMessage/onResume is called twice on Android/iOS #1669 show different message_ids when the duplicates come in. I'm seeing the same message_id.
  2. It seems to be Android only (since it's related to Automatic plugin registration breaks some cases flutter/flutter#49365), based on the Flutter team's understanding. I'm seeing this on iOS.

I think this is a separate issue.

@iapicca iapicca reopened this Feb 12, 2020
@red42
Copy link
Contributor

red42 commented Mar 30, 2020

It looks like the culprit is:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
    didReceiveNotificationResponse:(UNNotificationResponse *)response
             withCompletionHandler:(void (^)(void))completionHandler NS_AVAILABLE_IOS(10.0) {
  NSDictionary *userInfo = response.notification.request.content.userInfo;
  // Check to key to ensure we only handle messages from Firebase
  if (userInfo[kGCMMessageIDKey]) {
    [_channel invokeMethod:@"onResume" arguments:userInfo];
    completionHandler();
  }
}

Which gets called both when the app is in background (when we want to call "onResume") and when the app is not running at all.

Checking if there's no _launchNotification pending seems to be enough to avoid the problem.

  if (userInfo[kGCMMessageIDKey] && _launchNotification == nil) {
    [_channel invokeMethod:@"onResume" arguments:userInfo];
    completionHandler();
  }

I'm adding a PR with the change.

red42 added a commit to red42/flutterfire that referenced this issue Mar 30, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Fixes firebase#1911
userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: gets called even when the notification is opened with the app not running, so onResume was being fired when only onLaunch should be called.

The fix adds a check for a non-null _launchNotification to prevent the invocation of onResume.
@Ehesp Ehesp added impact: customer A bug with low impact (e.g. affecting only a few customers or has a workaround). (P3) plugin: messaging labels Apr 23, 2020
Ehesp pushed a commit that referenced this issue Oct 20, 2020
…irebaseUser (#1911)

* Sign-in methods now return `AuthResult` instead of `FirebaseUser`.

* Change `reauthenticateWithCredential` to return AuthResult as well

* Update tests
@Salakar Salakar added the Stale Issue with no recent activity label Apr 1, 2021
@Salakar
Copy link
Member

Salakar commented Apr 16, 2021

Closing in favour of trying the latest messaging version which has since this issue was created been heavily reworked to improve it along with detailed documentation: https://firebase.flutter.dev/docs/messaging/overview

If you still have a problem please raise a new GitHub issue with up to date information and code snippets if possible. Thanks.

@Salakar Salakar closed this as completed Apr 16, 2021
@firebase firebase locked and limited conversation to collaborators May 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
impact: customer A bug with low impact (e.g. affecting only a few customers or has a workaround). (P3) plugin: messaging Stale Issue with no recent activity type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants