如果你收到了这个弃用通知,说明你正在使用Firebase Cloud Messaging (FCM) 的直接通道,并且需要迁移到苹果推送通知服务 (APNs) 来处理下行消息。
下面是一些代码示例来解决这个问题:
- 修改推送通知的注册部分:
// 使用原来的 FCM 直接通道注册
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true
// 替换为使用 APNs 注册
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
- 实现推送通知的代理方法:
// FCM 直接通道的代理方法
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
// 处理 FCM 直接通道的下行消息
}
// APNs 的代理方法
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
// 处理 APNs 的下行消息
}
- 在 AppDelegate 中处理推送通知的注册和设备令牌更新:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// ...
// 注册推送通知
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (granted, error) in
if granted {
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
// ...
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// 更新设备令牌
Messaging.messaging().apnsToken = deviceToken
}
通过以上步骤的迁移,你将能够使用苹果推送通知服务 (APNs) 来处理下行消息,以替代已弃用的 FCM 直接通道。