尝试为 iOS10 设置推送通知。它以前是否适用于 10 以下的版本。
通读一些指南,我的设置是这样的:
// Register for the defined notifications
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = UIApplication.shared.delegate as! AppDelegate
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if error == nil {
UIApplication.shared.registerForRemoteNotifications()
}
}
} else {
// Fallback on earlier versions
let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: [autoCancelCategory])
UIApplication.shared.registerUserNotificationSettings(notificationSettings)
UIApplication.shared.registerForRemoteNotifications()
}
这是在我的一个 View Controller 中登录 ^ 时调用的。
现在在AppDelegate
中,它符合UNUserNotificationCenterDelegate
,我有这些方法:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Got a push!")
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Got a push!")
}
我还有:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let settings = UserDefaults.standard
settings.setValue(deviceToken, forKey: "deviceToken")
}
设备 token 作为数据
上传到服务器,这在以前的版本中运行良好。
我已经检查过服务器上的 token 与手机上的 token 相匹配。
我在Capabilities
中为所有目标启用了推送通知,我还勾选了Add the push Notifications entitlement to your entitlements file
。
没有收到任何东西尽管在推进中。
知道我在这里可能做错了什么吗?任何指针将不胜感激!
谢谢!
编辑:我还注意到推送通知在 iOS9 中不起作用。
最佳答案
您是否检查过 Build Settings > Code Signing Entitlements 是否引用了 entitlements 文件? [AppName].entitlements.
并且权利文件包含正确的信息:
(键:APS 环境,值:开发)
除此之外,您可以尝试重新生成推送证书。我们必须创建新证书才能在 IOS 10 升级后(但不是在沙箱环境中)在生产环境中运行。
关于ios - 在 iOS 10 和 iOS9 中接收推送通知时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39789521/