2
votes

I am using FCM for getting remote push notifications.

So while getting first FCM token from the below callback, I am able to trigger notification and receiving them properly.

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String)

After relaunching the app I am getting a new FCM token. No notifications are getting triggered by the new token.

Pre-requisites I'm following according to the documentation: https://firebase.google.com/docs/cloud-messaging/ios/client

Using FirebaseMessaging (3.3.0)

1
Did you updated the token stored in your backend server ? - Midhun MP
Yes, but I am testing using Firebase cloud messaging console. - Priyam Dutta
Are you getting any error from the console ? - Midhun MP
Unfortunately not getting any errors. - Priyam Dutta

1 Answers

3
votes

So I found the solution, Each time after getting FCM fresh token from the call back.

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String)

We have to re-register Remote push notification.

if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.badge, .alert, .sound]) {
            (granted, error) in
            if granted {
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            } else {
                //print("APNS Registration failed")
                //print("Error: \(String(describing: error?.localizedDescription))")
            }
        }
    } else {
        let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
        let setting = UIUserNotificationSettings(types: type, categories: nil)
        UIApplication.shared.registerUserNotificationSettings(setting)
        UIApplication.shared.registerForRemoteNotifications()
    }

FirebaseMessaging will re-configure the new FCM token with the device token.

Note: No, need to set explicitly device token. Since FirebaseMessaging is using method swizzling, it will automatically retrieve it from the delegate method.

func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)