取消来电



我想在来电取消后停止callkit通知,并且接收者尚未接听,因此我收到用户通过另一个具有missingCall键的pushkit通知取消呼叫。问题是第一次尝试从调用者取消呼叫是成功的,callkit通知被停止,但在那之后,任何呼叫都不会被取消,直到我完全终止应用程序,同样的事情只发生第一次成功

这是代码

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
print("didReceiveIncomingPushWith payload")

let payloadDictionary = payload.dictionaryPayload as NSDictionary
callOption.callData = payloadDictionary.value(forKey: "aps") as! [String : Any]
let user = callOption.callData["user"] as! [String  : Any]
let callerID = user["name"] as! String
let hasVideo  = true 
let callUpdate = CXCallUpdate()
let uuid = UUID()
callUpdate.remoteHandle = CXHandle(type: .generic, value: callerID)
callUpdate.hasVideo = hasVideo
DispatchQueue.global().async {
self.callOption.ringingApi(otherId: self.callOption.callData["snd_id"] as! String)
}
let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: nil)
if callOption.callData["typeCall"] as! String == "missingCall" {
self.endIncomingCall(userId: callOption.callData["snd_id"] as! String)
UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)

}
else {
self.displayIncomingCall(uuid: uuid, handle: callerID, hasVideo: hasVideo, userId: callOption.callData["snd_id"] as! String) { _ in
UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
}
}
}

这里的函数调用了上面的

func displayIncomingCall(uuid: UUID, handle: String, hasVideo: Bool, userId: String, completion: ((NSError?) -> Void)? = nil) {
providerDelegate?.reportIncomingCall(uuid: uuid, handle: handle, hasVideo: hasVideo, userId: userId, completion: completion)
}
func endIncomingCall( userId: String) {
providerDelegate?.reportTheCallerEndCall( userId: userId)
}

这是提供者委托

/// Use CXProvider to report the incoming call to the system
func reportIncomingCall(uuid: UUID, handle: String, hasVideo: Bool, userId: String, completion: ((NSError?) -> Void)? = nil) {
// Construct a CXCallUpdate describing the incoming call, including the caller.
let update = CXCallUpdate()
update.remoteHandle = CXHandle(type: .phoneNumber, value: handle)
update.hasVideo = hasVideo
provider.reportNewIncomingCall(with: uuid, update: update) { error in

if error == nil {
let call = Call(uuid: uuid, handle: handle, userId: userId)

self.callManager.add(call: call)
}

completion?(error as NSError?)
}
}
func reportTheCallerEndCall(userId: String) {
guard let callIndex = self.callManager.findCallIndexInCallsArray(userId: userId) else { return}
provider.reportCall(with: self.callManager.calls[callIndex].uuid, endedAt: nil, reason: .unanswered)
}

我可以看出你的代码中至少有两个问题。

  1. 您不需要使用后台任务来处理传入呼叫,它都是由PushKitCallKit框架自动处理的。

  2. 不调用pushRegistry(_:didReceiveIncomingPushWith:for:completion:)函数的completion处理程序。您可能知道,当您收到VoIP推送时,您必须报告一个新的来电,否则系统将不会在将来为您提供任何其他VoIP推送。如果你没有在reportNewIncomingCall函数的完成过程中调用推送completion处理程序,那么即使你这样做了,系统也会认为你没有报告调用。

self.displayIncomingCall(uuid: uuid, handle: callerID, hasVideo: hasVideo, userId: callOption.callData["snd_id"] as! String, completion: completion)

删除后台任务start/end,并将PushKit完成传递给呼入完成。

相关内容

  • 没有找到相关文章

最新更新