簡體   English   中英

當呼叫者停止來電時取消來電

[英]cancel incoming call when the caller stop it

我想在呼叫者被取消並且接收者尚未應答后停止來電中的呼叫套件通知,因此我收到用戶通過另一個具有 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)

移除后台任務開始/結束,並將 PushKit 完成傳遞給來電完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM