Kotlin

Gerenciar chamada

Receber chamada

Adicionar duas novas propriedades onGoingCallID para colocar a chamada atual em espera e callInviteID para manter o aviso de chamada recebida na parte superior da MainActivity classe:

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

Para monitorar chamadas recebidas, adicione um ouvinte de convite de chamada no final de onCreate método interno MainActivity classe:

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

O aplicativo agora ficará atento ao evento de chamada recebida. O código acima exibe os botões para atender e rejeitar a chamada quando o evento de chamada recebida é detectado. Observe que você está armazenando callInviteID referência para interagir posteriormente com a chamada.

Em seguida, logo abaixo do ouvinte de convite de chamada, adicione o ouvinte de desligamento do RTC; ele será acionado quando a pessoa que está ligando para você desligar e a chamada terminar.

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

Antes de poder realizar ações por meio da interface do usuário, você também precisa adicionar ouvintes aos botões. Adicione este código no final do arquivo onCreate dentro MainActivity classe:

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

Para atender a chamada, digite answerCall método interno MainActivity classe:

@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
        }
      }
    }
  }
}

Depois de atender a ligação, o end call O botão será exibido.

NOTA: O SuppressLint Essa anotação é usada por uma questão de simplicidade. No aplicativo de produção, você deve se certificar de que as permissões foram concedidas antes de atender a chamada.

Para rejeitar a chamada, adicione rejectCall método interno MainActivity classe:

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 encerrar a ligação, digite endCall método interno MainActivity classe:

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
}

Observe que, após rejeitar ou encerrar a chamada com sucesso, você define call redefinir o valor da propriedade para nulo.

Compilar e executar

Certifique-se de que o servidor de webhook que você configurou nas etapas anteriores ainda esteja em funcionamento.

Pressione o Ctrl + R chaves para compilar e executar o aplicativo. Ligue para o número.

Ligue para o número associado à sua inscrição, conforme indicado na etapa anterior.

Webhooks

À medida que você prossegue com a chamada, por favor, mude para o terminal e observe o /voice/answer endpoint chamado para recuperar o NCCO:

NCCO request: - caller: 447700900000 - callee: 442038297050

Além disso, à medida que a chamada passa por várias etapas, /voice/event está recebendo 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'
}
---