查看官方文档有用pushkit的方法,其实与其说是记录获取token的方法,不如说是记录pushkit的简单使用

1.这是官方文档下的代码做了版本修改,在主线成中注册voip推送

 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 = [PKPushType.voIP]

    }

2.协议方法

extension AppDelegate:PKPushRegistryDelegate{

    func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, forType type: PKPushType) {

        print(credentials.token)

        print(credentials)

        let nsdataStr = NSData.init(data: credentials.token)

        let datastr = nsdataStr.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: "")

        print(datastr)

    }

    func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenForType type: PKPushType) {

        

    }

    func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {

        print("收到推送")

        

    }

}

3.记得在后台设置voip模式

4.最重要的一点,voip推送也是推送,所以避免不了的做推送证书,然后才能获取到token,这里自己测试了感觉是这样,要是误解了还请大神些指点,谢谢

亲测收到voip推送的时候app自动重启,并执行了收到推送后的代码,这功能挺吊!这里多提一句,我这边后台是用java实验的,只需要证书p12文件/导出密码和token。