Rock
Rock

Reputation: 1510

PushKit Xcode 9.2 issue : Not able to get device token

I have tried to implement Pushkit in Xcode 9.2 But not able to get device token

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)")
    }

Upvotes: 3

Views: 771

Answers (1)

Mohamed Gafar
Mohamed Gafar

Reputation: 327

Make sure to convert it from bytes to string type

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)")

}

Upvotes: 4

Related Questions