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

Messaging didReceiveRegistrationToken called twice on startup after deleting and reinstalling app #6813

Closed
petkotodorov opened this issue Oct 23, 2020 · 6 comments
Assignees

Comments

@petkotodorov
Copy link

[REQUIRED] Step 1: Describe your environment

  • Xcode version: 12.0.1
  • Firebase SDK version: 6.34.0
  • Firebase Component: Messaging
  • Component version: 4.7.1
  • Installation method: CocoaPods

[REQUIRED] Step 2: Describe the problem

didReceiveRegistrationToken is being called twice on first start after deleting and installing app. In that case, app is not receiving notifications. It is not happening, when this is first ever install on the particular device.

Steps to reproduce:

  1. Run app for the very first time on this device and didReceiveRegistrationToken is called once with token (as expected)
  2. Delete app.
  3. Run app, freshly installed again and this time didReceiveRegistrationToken is called twice. First time with the token from previous installation. And a second later, didReceiveRegistrationToken called again with new token.
  4. In result is that, during that run app is not receiving notifications
  5. On each next run, didReceiveRegistrationToken is called once and all is good

If you have a downloadable sample project that reproduces the bug you're reporting, you will
likely receive a faster response on your issue.

Relevant Code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        
        Messaging.messaging().delegate = self
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
        application.registerForRemoteNotifications()
        return true
    }

 extension AppDelegate: UNUserNotificationCenterDelegate {
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        //Messaging.messaging().appDidReceiveMessage(userInfo)

        // Print full message.
        print("did receive RN: \(userInfo)")

        completionHandler(UIBackgroundFetchResult.newData)
    }
}

extension AppDelegate: MessagingDelegate {
    
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token from message delegate: \(String(describing: fcmToken))")

//        let dataDict:[String: String] = ["token": fcmToken ?? ""]
//        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
    }
    
}
@morganchen12
Copy link
Contributor

Can you share the Firebase/Messaging logs from your app launch?

@petkotodorov
Copy link
Author

petkotodorov commented Oct 23, 2020

@morganchen12 Of, course. Here's everything Firebase related (Analytics, Crashlytics, Messaging)

2020-10-23 10:41:39.221165-0700 ***-mwallet-ios[3562:1545878] 6.34.0 - [Firebase/Analytics][I-ACS023007] Analytics v.60900000 started
2020-10-23 10:41:39.221586-0700 ***-mwallet-ios[3562:1545878] 6.34.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see http://goo.gl/RfcP7r)
2020-10-23 10:41:39.229901-0700 ***-mwallet-ios[3562:1545881] 6.34.0 - [Firebase/Messaging][I-FCM001000] FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:
https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging
to ensure proper integration.
2020-10-23 10:41:39.230738-0700 ***-mwallet-ios[3562:1545647] [Firebase/Crashlytics] Version 4.6.2
2020-10-23 10:41:39.394452-0700 ***-mwallet-ios[3562:1545876] 6.34.0 - [Firebase/Analytics][I-ACS800023] No pending snapshot to activate. SDK name: app_measurement
2020-10-23 10:41:39.472991-0700 ***-mwallet-ios[3562:1545873] 6.34.0 - [Firebase/Analytics][I-ACS023012] Analytics collection enabled
2020-10-23 10:41:39.473136-0700 ***-mwallet-ios[3562:1545873] 6.34.0 - [Firebase/Analytics][I-ACS023220] Analytics screen reporting is enabled. Call +[FIRAnalytics logEventWithName:FIREventScreenView parameters:] to log a screen view event. To disable automatic screen reporting, set the flag FirebaseAutomaticScreenReportingEnabled to NO (boolean) in the Info.plist

@charlotteliang charlotteliang self-assigned this Oct 23, 2020
@charlotteliang
Copy link
Contributor

Can you share all Firebase debug Logs when you reproduce?

Here's how you can get the debug logs: To enable debug logging set the following application argument: -FIRDebugEnabled (see http://goo.gl/RfcP7r)

@petkotodorov
Copy link
Author

petkotodorov commented Oct 23, 2020

@chliangGoogle Here you go.. I created a gist, as it was rather long. Can paste it here also, if you need.
https://gist.github.com/petkotodorov/44855167a90c310c61857b7a76ab756c

@Xedart
Copy link

Xedart commented Oct 26, 2020

Same issue for me

@charlotteliang
Copy link
Contributor

Hi @petkotodorov This is expected. We recently discover that sometimes, the APNS token is collected from Apple with a slight delay after first token request was issued, so client has to issue a second token request with the apns token mapping in it. So you are getting that scenario which is why you are getting the token callback twice.

It wasn't happening before because there was a bug that the second token request is not triggered and was recently fixed in #6669.

This should not affect your work as long as you always keep your token updated with the Messaging didReceiveRegistrationToken: callback.

@firebase firebase locked and limited conversation to collaborators Nov 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants