1.添加权限:
https://www.jianshu.com/p/9afff11dd6af
2.添加代码:
//@available(iOS 8.0, *)
2.1调用voipRegistration
+ 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 = .init(arrayLiteral: PKPushType.voIP)
+ }
2.2 处理回调
extension AppDelegate: PKPushRegistryDelegate
{
func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType) {
let device = NSData(data: credentials.token)
let devStr = String.convertDataToHexStr(data:device)
let deviceId = device.description.replacingOccurrences(of:"<", with:"").replacingOccurrences(of:">", with:"").replacingOccurrences(of:" ", with:"")
HGLog("pushkit设备token:" + deviceId)
HGLog("pushkit设备token devStr:" + devStr)
}
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
var v = DateUtils.dateToString(Date())//ios 13 需要用这个转换
v.append(":pushkit-didReceiveIncomingPushWith")
UserDefaults.standard.setValue(v, forKey: "pushRegistryReceive")
HGLog("收到pushkit推送!")
HGLog(payload.dictionaryPayload)
// VideoCallManager.sharedInstance.playAlertSound()
}
}
3.申请证书
3.1 转换证书格式(cer证书转换.p12文件)
https://blog.csdn.net/xiejunna/article/details/60967293?utm_source=blogxgwz0
3.2 p12 转 pem
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
openssl pkcs12 -nocerts -out key.pem -in key.p12
cat cert.pem key.pem > ck.pem
4.pushkit发送端:
https://github.com/stefanhafeneger/PushMeBaby 下载其中的php脚本,修改devicetoken和ck.pem,密码
4.1或者mac上下载APNS Tool可以发送一般通知或者pushkit 通知;