didUpdatePushCredentials not get called

后端 未结 4 1367
温柔的废话
温柔的废话 2021-01-18 07:28

I want to implement VoIP notifications in my iOS application, But the didUpdatePushCredentials method never got called, I can\'t get the device token.

4条回答
  •  执念已碎
    2021-01-18 08:26

    For me the solution was to enable:

    P.S. I didn't forget to enable But enabling general pushes was important too. So full code is:

    import UIKit
    import PushKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            voipRegistration()
    
            return true
        }
    
        // Register for VoIP notifications
        func voipRegistration() {
            let mainQueue = DispatchQueue.main
            // Create a push registry object
            let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
            // Set the registry's delegate to self
            voipRegistry.delegate = self
            // Set the push type to VoIP
            voipRegistry.desiredPushTypes = [.voIP]
        }
    
    }
    
    extension AppDelegate: PKPushRegistryDelegate {
        func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
            let token = pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined()
            print("voip token = \(token)")
        }
    
        func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
            print("payload = \(payload.dictionaryPayload)")
        }
    }
    

提交回复
热议问题