Kotlin

Gestionar llamada

Recibir llamada

Añadir dos nuevas propiedades onGoingCallID para retener la llamada en directo actual y callInviteID para mantener la invitación de una llamada entrante en la parte superior de la MainActivity clase:

private var onGoingCallID: CallId? = null
private var callInviteID: CallId? = null

Para detectar llamadas entrantes, añade un detector de invitaciones de llamada al final de onCreate método inside MainActivity clase:

client.setCallInviteListener { callId, from, channelType ->
  callInviteID = callId
  runOnUiThread {
    answerCallButton.visibility = View.VISIBLE
    rejectCallButton.visibility = View.VISIBLE
    endCallButton.visibility = View.GONE
  }
}

Ahora la aplicación esperará a que se produzca el evento de llamada entrante. El código anterior muestra los botones para contestar y rechazar la llamada cuando se recibe el evento de llamada entrante. Fíjate en que estás almacenando callInviteID referencia para interactuar más adelante con la llamada.

A continuación, debajo del oyente de invitación a llamar, añada el oyente de colgar RTC, que se encargará de cuando la persona que le llama cuelgue y finalice la llamada.

client.setOnCallHangupListener { callId, callQuality, isRemote ->
  onGoingCallID = null
  answerCallButton.visibility = View.GONE
  rejectCallButton.visibility = View.GONE
  endCallButton.visibility = View.GONE
}

Antes de que puedas realizar acciones usando UI también necesitas añadir listeners a los botones. Añade este código en el onCreate dentro MainActivity clase:

answerCallButton.setOnClickListener { answerCall() }
rejectCallButton.setOnClickListener { rejectCall() }
endCallButton.setOnClickListener { endCall() }

Para responder a la llamada, añada answerCall método inside MainActivity clase:

@SuppressLint("MissingPermission")
private fun answerCall() {
  callInviteID?.let {
    client.answer(it) {
      err ->
      when {
        err != null -> {
          connectionStatusTextView.text = err.localizedMessage
        }
        else -> {
          onGoingCallID = it
          answerCallButton.visibility = View.GONE
          rejectCallButton.visibility = View.GONE
          endCallButton.visibility = View.VISIBLE
        }
      }
    }
  }
}

Tras responder a la llamada, el end call Aparecerá el botón.

NOTA: En SuppressLint se utiliza por simplicidad. En la aplicación de producción debes asegurarte de que se conceden los permisos antes de responder a la llamada.

Para rechazar la llamada añada rejectCall método inside MainActivity clase:

private fun rejectCall() {
    callInviteID?.let {
      client.reject(it) { err ->
        when {
          err != null -> {
            connectionStatusTextView.text = err.localizedMessage
          }
          else -> {
            answerCallButton.visibility = View.GONE
            rejectCallButton.visibility = View.GONE
            endCallButton.visibility = View.GONE
          }
        }
      }
    onGoingCallID = null
  }
}

Para finalizar la llamada añada endCall método inside MainActivity clase:

private fun endCall() {
  onGoingCallID?.let {
    client.hangup(it) {
      err ->
      when {
        err != null -> {
          connectionStatusTextView.text = err.localizedMessage
        }
        else -> {
          answerCallButton.visibility = View.GONE
          rejectCallButton.visibility = View.GONE
          endCallButton.visibility = View.GONE
        }
      }
    }
  }
  onGoingCallID = null
}

Ten en cuenta que, tras rechazar o finalizar correctamente la llamada, debes establecer call restablecer el valor de la propiedad a «null».

Construir y ejecutar

Asegúrate de que el servidor de webhooks que has configurado en los pasos anteriores siga en funcionamiento.

Pulse el botón Ctrl + R para crear y ejecutar la aplicación. Llama al número.

Llame al número asociado a su solicitud en el paso anterior.

Webhooks

A medida que avance en la llamada, por favor, cambie al terminal y fíjese en el /voice/answer punto final al que se realiza la llamada para recuperar el NCCO:

NCCO request: - caller: 447700900000 - callee: 442038297050

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

EVENT:
{
  headers: {},
  from: '447700900000',
  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: '447700900000',
  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: '447700900000',
  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: '447700900000',
  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: '447700900000',
  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: '447700900000',
  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: ' 447700900000',
  to: '442038297050',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'completed',
  direction: 'inbound',
  timestamp: '2021-03-29T21:20:17.514Z'
}
---