Skip to content

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

Closed
@RabbitKabong

Description

@RabbitKabong

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);
      },
    );

Activity

iapicca

iapicca commented on Feb 3, 2020

@iapicca

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

RabbitKabong

RabbitKabong commented on Feb 3, 2020

@RabbitKabong
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.

red42

red42 commented on Mar 30, 2020

@red42
Contributor

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.

added a commit that references this issue on Mar 30, 2020
799ce36
added
impact: customerA bug with low impact (e.g. affecting only a few customers or has a workaround). (P3)
on Apr 23, 2020
Salakar

Salakar commented on Apr 16, 2021

@Salakar
Member

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.

locked and limited conversation to collaborators on May 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleIssue with no recent activityimpact: customerA bug with low impact (e.g. affecting only a few customers or has a workaround). (P3)plugin: messagingtype: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @Ehesp@red42@Salakar@iapicca@RabbitKabong

      Issue actions

        [firebase_messaging] Both onMessage and onLaunch called with same gcm.message_id · Issue #1911 · firebase/flutterfire