我正在使用以下代码为我的应用注册远程通知
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories)
application.registerUserNotificationSettings(notificationSettings)
application.registerForRemoteNotifications()
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("token: \(deviceToken)")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Error : Fail to register to APNS : \(error)")
}
对于 iPhone 6,它不会调用方法 didRegisterForRemoteNotificationsWithDeviceToken
或 didFailToRegisterForRemoteNotificationsWithError
,对于其他设备,如 5c/4s,它正在注册并打印 deviceToken
最初我认为这是操作系统问题,但后来,我检查了具有相同版本 9.3.2 的其他设备,它有效。
有人遇到同样的问题吗?
最佳答案
把这段代码写在 didFinishLaunchingWithOptions 中用于远程通知。我认为它会工作正常
if #available(iOS 8.0, *) {
let userNotificationTypes:UIUserNotificationType = ([UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound])
if UIApplication.sharedApplication().respondsToSelector("isRegisteredForRemoteNotifications") {
// iOS 8 Notifications
let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
} else {
// iOS < 8 Notifications
UIApplication.sharedApplication().registerForRemoteNotificationTypes([.Badge, .Sound, .Alert])
}
print("Push notifications setup complete")
}
快乐编码。
关于ios - 无法使用 swift 在 iPhone 6 中注册远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36886069/