快速获取设备令牌

时间:2016-07-19 17:55:16

标签: ios swift push-notification apple-push-notifications push

突然,两天前我停止接收到iPhone的推送通知(我每隔5-10分钟从我的网站发送一次自动推送通知)。

我打开了我的Xcode项目,并尝试接收我的设备令牌以检查一切是否正常,但是没有打印。 它要求我在我的设备上启用推送通知,当我按“确定”时没有任何反应。

代码如下,在app delegate中:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    registerForPushNotifications(application)
    return true
}

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

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    print("Device Token:", tokenString)
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Failed to register:", error)
}

1 个答案:

答案 0 :(得分:1)

我似乎通过登录我的“苹果开发者”帐户并同意更新的服务条款来解决此问题。

在我这样做了大约1小时后,我又开始接收推送通知。

感谢您的帮助!