【问题标题】:Getting empty device token in iPhone 5 and iPhone SE在 iPhone 5 和 iPhone SE 中获取空设备令牌
【发布时间】:2017-05-09 05:59:07
【问题描述】:

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

有什么帮助吗?

【问题讨论】:

  • 从设备中删除应用并重新安装。
  • 查看stackoverflow.com/a/37967012/3901620中的requestAuthorization
  • 他们的操作系统版本不同吗?
  • @BalajiGalave 在 6 中,我的操作系统是 10.1.1,它运行成功,但在 5 中,操作系统是 10.2.1,它返回空
  • 你实现didRegisterUserNotificationSettings了吗?

标签: ios iphone swift push-notification swift2


【解决方案1】:

我已经在我的AppDelegate 中处理了didRegisterUserNotificationSettings

这样

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

这解决了@Lion 提出的问题

【讨论】:

  • 你的意思是你只是把.None改成了[],你的问题就解决了?!
  • 我之前没有处理过didRegisterUserNotificationSettings。处理完这个问题后,我的问题解决了@Honey
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多