Receive a Call
This guide covers how to receive a call with the Vonage Client SDK. Before you begin, make sure you added the SDK to your app and (Android, iOS, JS).
NOTE: On iOS it is expected that you use CallKit for incoming and outgoing calls. Please follow the push notification guide.
Receive a Call Invite
In order to receive an incoming in-app call, you should listen for incoming call invites:
To receive an incoming in-app call, listen for the callInvite event:
// After creating a session
client.on("callInvite", (callId, from, channelType) => {
// Answer / Reject Call
console.log( { callId, from, channelType } );
});
client.setCallInviteListener { callId, from, channelType ->
// Handling incoming call invite
}
Add the current ViewController, or similar, as a delegate for the voice client:
ViewController will now have to conform to VGVoiceClientDelegate, the didReceiveInviteForCall function will be called when there is an incoming call invite. Keep reference to the callId as this will be needed to perform further operations:
Then, you’ll be able to answer or reject the invite.
Answer
// After creating a session
client.answer(callId)
.then(() => {
console.log("Success answering call.");
})
.catch(error => {
console.error("Error answering call: ", error);
});
client.answer(callID) {
err ->
when {
err != null -> {
// Handle invite answer error
}
else -> {
// Handle active call
}
}
}
Reject
// After creating a session
client.reject(callId)
.then(() => {
console.log("Success rejecting call.");
})
.catch(error => {
console.error("Error rejecting call: ", error);
});
client.reject(callID) {
err ->
when {
err != null -> {
// Handle inviate reject error
}
}
}
Hang Up
For an on-going call:
// After creating a session
client.hangup(callId)
.then(() => {
console.log("Success hanging up call.");
})
.catch(error => {
console.error("Error hanging up call: ", error);
});
client.hangup(callID) {
err ->
when {
err != null -> {
// Handle hang up error
}
}
}
Listen For Call Events
To see updates on the state of a call, for example, to know if the other member answered or hung up the call, you should listen to leg status events.
To see updates on the state of the call and its members:
// After creating a session
client.on("legStatusUpdate", (callId, legId, status) => {
console.log({callId, legId, status});
});
client.setOnLegStatusUpdate { callId, legId, status ->
// Call leg updates
}
The didReceiveLegStatusUpdateForCall function will update you on changes to call legs for the active call: