首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PushKit Xcode9.2问题:无法获取设备令牌

PushKit Xcode9.2问题:无法获取设备令牌
EN

Stack Overflow用户
提问于 2018-03-22 19:58:52
回答 1查看 661关注 0票数 3

我曾尝试在Xcode9.2中实现Pushkit,但无法获得设备令牌

代码语言:javascript
复制
class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
        UIApplication.shared.registerForRemoteNotifications()

        NSLog("app launched with state \(application.applicationState)")
        voipRegistration()
        return true
    }

    func voipRegistration() {
        // Create a push registry object
        let voipRegistry: PKPushRegistry = PKPushRegistry(queue: DispatchQueue.main)
        // Set the registry's delegate to self
        voipRegistry.delegate = self
        // Set the push type to VoIP
        voipRegistry.desiredPushTypes = Set([PKPushType.voIP])
    }

    /*
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        //register for voip notifications
        let voipRegistry = PKPushRegistry(queue: DispatchQueue.main)
        voipRegistry.desiredPushTypes = Set([PKPushType.voIP])
        voipRegistry.delegate = self;
    }
    */

    func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
        //print out the VoIP token. We will use this to test the notification.
        NSLog("voip token: \(pushCredentials.token)")
    }
EN

回答 1

Stack Overflow用户

发布于 2018-10-01 15:30:40

确保将其从字节类型转换为字符串类型

代码语言:javascript
复制
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {

    //print out the VoIP token.
    let token = pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined()
    print("voip token: \(token)")

}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49428138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档