4

By using one of the sample video calling app provided by Twilio (VideoCallKitQuickStart), I am trying to trigger an incoming call by sending a VoIP notification to the App. But the App doesn't trigger an incoming call. I also tried keeping the App opened while sending a VoIP notification and the App crashes, by throwing the below exception

NSInvalidArgumentException: Attempt to insert non-property list object 'PKPushPayload: 0x16e44af0' for key payload

Could someone, please help me or point me in the right direction on how to trigger an incoming call in the App, when a VoIP notification is received.

Below is my code in the ViewController.swift file

 func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
        // Process the received push

        self.reportIncomingCall(uuid: UUID(), roomName: "testRoom", completion: nil)
    } 

func reportIncomingCall(uuid: UUID, roomName: String?, completion: ((NSError?) -> Void)? = nil) {

    let callHandle = CXHandle(type: .generic, value: roomName ?? "")
    let callUpdate = CXCallUpdate()
    callUpdate.remoteHandle = callHandle
    callUpdate.supportsDTMF = false
    callUpdate.supportsHolding = true
    callUpdate.supportsGrouping = false
    callUpdate.supportsUngrouping = false
    callUpdate.hasVideo = true

    callKitProvider.reportNewIncomingCall(with: uuid, update: callUpdate) { error in
        if error == nil {
            NSLog("Incoming call successfully reported.")
        } else {
            NSLog("Failed to report incoming call successfully: \(error?.localizedDescription).")
        }
        completion?(error as? NSError)
    }
}
SpaceX
  • 2,814
  • 2
  • 42
  • 68

3 Answers3

1

Posting late answer but it may helpful for someone.

Following code I did to handle voice incoming call.

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
    NSLog("pushRegistry:didReceiveIncomingPushWithPayload:forType:")
    print(payload)
    if (type == PKPushType.voIP) {
        TwilioVoice.handleNotification(payload.dictionaryPayload, delegate: self)

        pushKitPushReceivedWithPayload(payload: payload)
    }
}
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
    NSLog("pushRegistry:didReceiveIncomingPushWithPayload:forType:completion:")

    if (type == PKPushType.voIP) {
        TwilioVoice.handleNotification(payload.dictionaryPayload, delegate: self)

        pushKitPushReceivedWithPayload(payload: payload)
    }

    completion()
}

func pushKitPushReceivedWithPayload(payload: PKPushPayload){
    if UIApplication.shared.applicationState != .active{
        let msgType = payload.dictionaryPayload["twi_message_type"] as? String
        if let messageType = msgType{
            if messageType == "twilio.voice.call"{
                fireLocalNotificationForVoiceCall(didStart: true)
            }else if messageType == "twilio.voice.cancel"{
                fireLocalNotificationForVoiceCall(didStart: false)
            }
        }
    }
}

Below are the delegate methods of call kit I have added

extension AppDelegate : TVONotificationDelegate, TVOCallDelegate
{
  func callInviteReceived(_ callInvite: TVOCallInvite) 
  {
   if (callInvite.state == .pending) 
   {
        //code
   } 
   else if (callInvite.state == .canceled) 
   {
        //code
   }
  }
  func handleCallInviteReceived(_ callInvite: TVOCallInvite) 
  {
        //code
  }

  func handleCallInviteCanceled(_ callInvite: TVOCallInvite) 
  {
        //code
  }
}

I have followed this tutorial provided by twilio - https://github.com/twilio/voice-quickstart-swift

Go through this tutorial and it will work.

iDeveloper
  • 607
  • 5
  • 25
  • Above code are you posted for voice call. but i need for Video call. Would you please send me code for only video call and also please send me code fireLocalNotificationForVoiceCall(didStart: true) function code –  Feb 15 '19 at 05:36
0

Twilio developer evangelist here.

I'm not particularly good with iOS, but taking a quick look at the documentation for the PKPushRegistryDelegate it looks like your pushRegistry function definition isn't right.

It should be

func pushRegistry(_ registry: PKPushRegistry, 
    didReceiveIncomingPushWith payload: PKPushPayload, 
    forType type: PKPushType)

That is, didReceiveIncomingPushWith rather than didReceiveIncomingPushWithPayload.

Alternatively, does it have anything to do with the fact that you're casting forType to String?

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Hello, I've implemented the twilio webrtc GO app inside my app. I wanted to integrate it with CallKit. Like when userA joins the room, a notification is sent to userB and it shows the CallKit UI, basically receiving the call and when he accepts the call, he joins the room. Where should I start to achieve this feature? I can send notification using Firebase Cloud Function and am successfully able to send them. Your help would really be appreciated – Arjun Jan 21 '22 at 12:11
  • HI @Arjun, I'd recommend you take a look through the [quick start repository](https://github.com/twilio/video-quickstart-ios/), particularly the [CallKit example](https://github.com/twilio/video-quickstart-ios/tree/master/VideoCallKitQuickStart). – philnash Jan 23 '22 at 22:28
  • Hey @philnash I am able to simulate the callkit call, as shown in the CallKit example, I've also implemented pushKit in my app and have the voip certificate. I don't seem to find a way to send voip push notification? Could you please help me how should I proceed ahead? Is there a way voip push could be sent using firebase cloud functions? May be by adding certain flag to payload or something? – Arjun Jan 25 '22 at 10:08
  • Have you followed [the instructions for setting up push notifications here](https://www.twilio.com/docs/voice/sdks/ios/faq#incoming-calls-and-registration-for-push-notifications)? You need to set up your push certificate in the console and then Twilio sends the notifications to your app. – philnash Jan 25 '22 at 10:14
  • Hello @philnash. Is this solution possible with PushKit + Firebase? https://stackoverflow.com/questions/37326450/does-firebase-cloud-messaging-support-voip-pushkit-services#:~:text=As%20far%20as%20I%20understand,instance%2C%20no%20change%20is%20required! or do I have to stick with the twilio implementation? – Arjun Jan 27 '22 at 06:45
  • 1
    My apologies, I confused myself and started talking about the Voice SDK. For Video you need to send the push notifications yourself. If you can send a CallKit notification from Firebase then you can integrate it with Twilio Video. However, I haven’t done that myself so I don’t have any further advice. – philnash Jan 27 '22 at 08:43
  • I'm able to send normal notification using firebase and am able to trigger the callkit when app is in foreground. However, when app is in background, the callkit is not triggered. I think voip push notification is needed to wake up the app when in background or closed. Any help to achieve this would really be helpful. Thanks – Arjun Jan 27 '22 at 08:50
  • 1
    I would love to help, but I don’t know. Are you not able to send a VOIP notification from Firebase? – philnash Jan 27 '22 at 08:52
  • No. I tried solution over here https://stackoverflow.com/questions/37326450/does-firebase-cloud-messaging-support-voip-pushkit-services#:~:text=As%20far%20as%20I%20understand,instance%2C%20no%20change%20is%20required but I'm not able to receive the voip push notification – Arjun Jan 27 '22 at 08:55
  • 1
    I have no experience with this, so I do not know how to help further. – philnash Jan 27 '22 at 08:55
0

Swift 3.0

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
    NSLog("pushRegistry:didReceiveIncomingPushWithPayload:forType:")

    if (type == PKPushType.voIP) {
        print(payload.dictionaryPayload)
        VoiceClient.sharedInstance().handleNotification(payload.dictionaryPayload, delegate: self)
    }
}

And please don't make any changes in payload without modifying it in order for the SDK to extract the incoming call info out of the payload so that the SDK can notify the application with incoming calls

krish
  • 3,856
  • 2
  • 24
  • 28