所以,我找到了解决方案,每次从回调中获取FCM新令牌后执行。
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String)
我们需要重新注册远程推送通知。
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 {
}
}
} 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将使用方法交换重新配置新的FCM令牌和设备令牌。
注意:不需要显式设置设备令牌。由于FirebaseMessaging使用了方法交换技术,它会自动从委托方法中检索设备令牌。
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)