iOS 16推送通知注册异常问题求助
iOS 16推送通知注册异常问题求助
各位同行们,我最近碰到一个iOS 16推送通知注册的奇怪问题,折腾好久没解决,来求助大家!
情况是这样的:在iOS 14和iOS 15系统上,我注册推送通知后,didRegisterForRemoteNotificationsWithDeviceToken或者didFailToRegisterForRemoteNotificationsWithError这两个代理方法都能正常触发,完全符合预期。但到了iOS 16上,注册之后这两个方法完全没有被调用,更离谱的是UIApplication.shared.isRegisteredForRemoteNotifications居然返回True——这显然是不对的,因为我根本没拿到设备令牌也没收到错误回调。
下面是我当前的相关代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { registerForPushNotifications() return true } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) } let token = tokenParts.joined() print("Device Token: \(token)") } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("NOTIFICATION ERROR: \(error)") } func registerForPushNotifications() { UNUserNotificationCenter.current() .requestAuthorization( options: [.alert, .sound, .badge]) { [weak self] granted, _ in print("Permission granted: \(granted)") guard granted else { return } self?.getNotificationSettings() } } func getNotificationSettings() { DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() } }
有没有大佬遇到过类似的情况?或者能帮我看看代码里有没有iOS 16相关的遗漏点?麻烦指点一下,万分感谢!
备注:内容来源于stack exchange,提问作者Jacob9812
相关产品推荐
相关产品推荐

