我没有在iPhone5(10.2.1)和SE(10.3.1)以及其他5个相关设备中获得设备令牌,但也成功地在所有iPhone6(包括plus、SE)和7以及7+中获得令牌。
这是我注册的代码
func registerForPushNotification(let application: UIApplication) {
let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
application.registerUserNotificationSettings(pushNotificationSettings)
application.applicationIconBadgeNumber = 0
if pushNotificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}
这是我收集代币的代码
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
let deviceTokenString: String = ( deviceToken.description as NSString )
.stringByTrimmingCharactersInSet( characterSet )
.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
print( deviceTokenString )
}
有什么帮助吗?
最佳答案
我在我的didRegisterUserNotificationSettings
中处理过AppDelegate
这样地
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != [] {
application.registerForRemoteNotifications()
}
}
这解决了“狮子”提出的问题
关于ios - 在iPhone 5和iPhone SE中获取空设备 token ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43862158/