Ryan
Ryan

Reputation: 4884

Async task after `didReceiveIncomingPushWithPayload` and before reportNewIncomingCall

When I get a PushKit notification, I have to call CXProvider().reportNewIncomingCall immediately or the app will be killed with the below message.

Killing app because it never posted an incoming call 
to the system after receiving a PushKit VoIP push.

Is there a way to start an asynchronous task after I receive a notification, and call CXProvider().reportNewIncomingCall when the asynchronous task is done? I tried to use the background task to avoid the app gets killed but it didn't work. Here's what I tried:

var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
    // Start a background task to extend execution time
    backgroundTaskID = UIApplication.shared.beginBackgroundTask(withName: "HandleVoIPPush") {
        // End task if time expires
        UIApplication.shared.endBackgroundTask(self.backgroundTaskID)
        self.backgroundTaskID = .invalid
    }
    
    // Start your async task
    asyncTask { _ in
        provider.reportNewIncomingCall(with: callUUID, update: update) { _ in }
        
        // End the background task
        UIApplication.shared.endBackgroundTask(self.backgroundTaskID)
        self.backgroundTaskID = .invalid
        
        // Call the completion handler to let the system know you're done
        completion()
    }
}

Upvotes: 1

Views: 39

Answers (0)

Related Questions