Проблема PushKit Xcode 9.2: не удалось получить токен устройства

Я попытался реализовать Pushkit в Xcode 9.2, но не смог получить токен устройства

Класс 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)")
}

1 ответ

Убедитесь, что он преобразован из байтов в строковый тип

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

}
Другие вопросы по тегам