【发布时间】:2016-09-30 10:36:04
【问题描述】:
尝试为 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()
}
这是在我的一个视图控制器中登录 ^ 时调用的。
现在在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 中不起作用。
【问题讨论】:
-
可能是操作系统问题。请尝试重新启动设备。 twitter.com/yogye7/status/781340933228945408
标签: ios swift apple-push-notifications swift3