在iPhone 5和iPhone SE中获取空设备令牌

时间:2017-05-09 05:59:08

标签: ios iphone swift push-notification swift2

我没有在iPhone 5(10.2.1)和SE(10.3.1)以及其他5个相关设备中获取设备令牌,但在所有iPhone 6(包括加号,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 )
}

有任何帮助吗?

1 个答案:

答案 0 :(得分:0)

我已在didRegisterUserNotificationSettings

中处理了AppDelegate

喜欢这个

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != [] {
        application.registerForRemoteNotifications()
    }   
}

这解决了@Lion

建议的问题