iOS 同步推是指 iOS 设备上的推送消息同步到其他设备上,以实现在不同设备上收到相同的推送消息。是使用 iOS 同步推的代码:
iOS 同步推是指 iOS 设备上的推送消息同步到其他设备上,以实现在不同设备上收到相同的推送消息。
是使用 iOS 同步推的代码:
// 在 AppDelegate 中注册远程推送
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 注册远程推送
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("注册远程推送成功")
} else {
print("注册远程推送失败")
}
}
application.registerForRemoteNotifications()
return true
}
// 在 AppDelegate 中处理远程推送
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// 同步推送
let center = UNUserNotificationCenter.current()
center.getDeliveredNotifications { (notifications) in
if notifications.count > 0 {
for notification in notifications {
// 将收到的推送同步到其他设备
}
}
}
completionHandler(.newData)
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(52条)