正在尝试为iOS10设置推送通知。有它先前通过一些导游我的设置是像这样工作版本低于10
阅读:在iOS10和iOS9中接收推送通知时遇到问题
// 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()
}
这是在我的视图控制器的一个呼吁登录^。
现在在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")
}
设备令牌被上传到服务器Data
,和这对以前的版本来说工作得很好。
我检查过服务器上的令牌与手机上的令牌匹配。
我已经为Capabilities
中的所有目标启用了推送通知,我也打勾了Add the push Notifications entitlement to your entitlements file
。
即使在推送时也没有收到任何东西。
任何想法我可能在这里做错了吗?任何指针将非常感激!
谢谢!
编辑:我也注意到,推送通知不适用于iOS9。
这可能是操作系统问题。请尝试重新启动设备。 https://twitter.com/yogye7/status/781340933228945408 –