Reputation: 702
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)
Upvotes: 3
Views: 2360
Reputation: 702
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)
Upvotes: 3