Skip to content

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

Closed
@petkotodorov

Description

@petkotodorov

[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.
    }
    
}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions