【Swift】PushKit(voip开发)--之获取pushT

2022-12-18  本文已影响0人  哗啦啦啦呃

记录一下我改的一个关于获取的pushToken在iOS15上能生效,但是在iOS16上不生效的bug

首先,获取token是在 pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {} 代理方法中的。

原获取方法:

func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
        print("voip 应用启动此代理方法会返回设备Token")
        guard type == PKPushType.voIP else {
            print("voip type is not voIP")
            return
        }
        if pushCredentials.token.count <= 0 {
            print("voip token nil")
            return
        }
var hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(pushCredentials.token.bytes),
                                                            count: pushCredentials.token.count).map { String(format: "%02x", $0) }.joined(separator: "")
        hexString = hexString.replacingOccurrences(of: "<", with: "")
        hexString = hexString.replacingOccurrences(of: ">", with: "")
        hexString = hexString.replacingOccurrences(of: " ", with: "")
        print("voip token1 = \(hexString)")
}

这样获取在iOS15之前是有用的,但是因为iOS16对UnsafePointer(不安全指针)方法应该是有所修改,导致最后得不到正确的token,voip也就没办法推送了。
所以需要token转字符串的方法

新的方法:

func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
        print("voip 应用启动此代理方法会返回设备Token")
        guard type == PKPushType.voIP else {
            print("voip type is not voIP")
            return
        }
        if pushCredentials.token.count <= 0 {
            print("voip token nil")
            return
        }
        
        var hexString = pushCredentials.token.bytes.map{ String(format: "%02x", $0) }.joined(separator: "")
        hexString = hexString.replacingOccurrences(of: "<", with: "")
        hexString = hexString.replacingOccurrences(of: ">", with: "")
        hexString = hexString.replacingOccurrences(of: " ", with: "")
        print("voip token1 = \(hexString)")
    }

Over

上一篇 下一篇

猜你喜欢

热点阅读