Swift

Recibir una llamada

En la parte superior del ViewController debajo de la clase client añada una propiedad string que contenga una referencia a cualquier llamada en curso.

class ViewController: UIViewController {
    
    let connectionStatusLabel = UILabel()
    let client = VGVoiceClient()
    var callID: String?
    ...
}

Cuando la aplicación reciba una llamada querrá dar la opción de aceptar o rechazar la llamada. Para ello, añada el campo displayIncomingCallAlert a la función ViewController clase.

class ViewController: UIViewController {
    ...
    func displayIncomingCallAlert(callID: String, caller: String) {
        let alert = UIAlertController(title: "Incoming call from", message: caller, preferredStyle: .alert)
        
        alert.addAction(UIAlertAction(title: "Answer", style: .default, handler: { _ in
            self.client.answer(callID) { error in
                if error == nil {
                    self.callID = callID
                }
            }
        }))
        
        alert.addAction(UIAlertAction(title: "Reject", style: .destructive, handler: { _ in
            self.client.reject(callID) { error in
                if let error {
                    self.connectionStatusLabel.text = error.localizedDescription
                }
            }
        }))
        
        self.present(alert, animated: true, completion: nil)
    }
}

En displayIncomingCallAlert toma como parámetros un identificador de llamada y un llamante. Tenga en cuenta que en la función UIAlertAction por responder a la invitación, si tiene éxito asignará el callID a la propiedad desde antes

Para utilizar displayIncomingCallAlert debe utilizar la función VGVoiceClientDelegate que tiene una función que se llamará cuando el cliente reciba una entrada VGVoiceInvite.

extension ViewController: VGVoiceClientDelegate {
  ...

  func voiceClient(_ client: VGVoiceClient, didReceiveInviteForCall callId: String, from caller: String, withChannelType type: VGVoiceChannelType) {
      DispatchQueue.main.async { [weak self] in
          self?.displayIncomingCallAlert(callID: callId, caller: caller)
      }
  }
  
  func voiceClient(_ client: VGVoiceClient, didReceiveInviteCancelForCall callId: String, with reason: VGVoiceInviteCancelReason) {
      DispatchQueue.main.async { [weak self] in
          self?.dismiss(animated: true)
      }
  }
  
  func voiceClient(_ client: VGVoiceClient, didReceiveHangupForCall callId: String, withQuality callQuality: VGRTCQuality, reason: VGHangupReason) {
      DispatchQueue.main.async { [weak self] in
          self?.callID = nil
          self?.connectionStatusLabel.text = "Call Ended"
      }
  }
}

También puede aplicar la función didReceiveHangupForCall función delegada que se llama si se cuelga la llamada.

NOTA: Además, asegúrese de que el servidor webhook que construyó en los pasos anteriores sigue funcionando.

Pulse Cmd + R para construir y ejecutar de nuevo, cuando llame al número vinculado con su aplicación desde antes se le presentará una alerta. Puedes descolgar y la llamada se conectará.

Incoming call alert

Webhooks

A medida que avanza con la llamada, por favor, cambie al terminal y observe el /voice/answer al que se llama para recuperar la NCCO:

NCCO request:
  - caller: 441234567890

Además, a medida que la convocatoria avanza por las distintas fases, /voice/event se envían eventos:

EVENT:
{
  headers: {},
  from: '441234567890',
  to: '442038297050',
  uuid: '0779a56d002f1c7f47f82ef5fe84ab79',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'ringing',
  direction: 'inbound',
  timestamp: '2021-03-29T21:20:05.582Z'
}
---
EVENT:
{
  headers: {},
  from: '441234567890',
  to: '442038297050',
  uuid: '0779a56d002f1c7f47f82ef5fe84ab79',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'started',
  direction: 'inbound',
  timestamp: '2021-03-29T21:20:05.582Z'
}
---
EVENT:
{
  start_time: null,
  headers: {},
  rate: null,
  from: '441234567890',
  to: '442038297050',
  uuid: '0779a56d002f1c7f47f82ef5fe84ab79',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'answered',
  direction: 'inbound',
  network: null,
  timestamp: '2021-03-29T21:20:06.182Z'
}
---
EVENT:
{
  from: '441234567890',
  to: 'Alice',
  uuid: '944bf4bf-8dc7-4e23-86b2-2f4234777416',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'started',
  direction: 'outbound',
  timestamp: '2021-03-29T21:20:13.025Z'
}
---
EVENT:
{
  start_time: null,
  headers: {},
  rate: null,
  from: '441234567890',
  to: 'Alice',
  uuid: '944bf4bf-8dc7-4e23-86b2-2f4234777416',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'answered',
  direction: 'outbound',
  network: null,
  timestamp: '2021-03-29T21:20:13.025Z'
}
---
EVENT:
{
  headers: {},
  end_time: '2021-03-29T21:20:16.000Z',
  uuid: '944bf4bf-8dc7-4e23-86b2-2f4234777416',
  network: null,
  duration: '5',
  start_time: '2021-03-29T21:20:11.000Z',
  rate: '0.00',
  price: '0',
  from: '441234567890',
  to: 'Alice',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'completed',
  direction: 'outbound',
  timestamp: '2021-03-29T21:20:17.574Z'
}
---
EVENT:
{
  headers: {},
  end_time: '2021-03-29T21:20:18.000Z',
  uuid: '0779a56d002f1c7f47f82ef5fe84ab79',
  network: 'GB-FIXED',
  duration: '12',
  start_time: '2021-03-29T21:20:06.000Z',
  rate: '0.00720000',
  price: '0.00144000',
  from: ' 441234567890',
  to: '442038297050',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'completed',
  direction: 'inbound',
  timestamp: '2021-03-29T21:20:17.514Z'
}
---