Receive an Inbound Call
Inbound calls are delivered to clients with active sessions after the Vonage API executes your answer webhook flow.
Inbound Path
Warning: Register listeners first Register call invite listeners before creating or reconnecting a session, so incoming invites are not missed.
Handling Inbound Call Invites
client.on('callInvite', async (callId, from, channelType) => {
console.log(`Received call invite from ${from} on ${channelType}`);
// To accept the call:
await client.answer(callId);
// Or to reject the call:
// await client.reject(callId);
});
client.on('callInviteCancel', (callId, reason) => {
if (reason === CancelReason.AnsweredElsewhere) {
console.log(`Call ${callId} was answered elsewhere`);
} else if (reason === CancelReason.RejectedElsewhere) {
console.log(`Call ${callId} was rejected elsewhere`);
} else if (reason === CancelReason.RemoteCancel) {
console.log(`Call ${callId} was cancelled by the caller`);
} else if (reason === CancelReason.RemoteTimeout) {
console.log(`Call ${callId} timed out`);
}
});
// Answer Call
client.setCallInviteListener {callId, from, channelType ->
println("Incoming Call from $from by channel $channelType")
client.answer(callId) {
it?.let {err ->
// Handle Error in answering call
println("Error in answering call: $err")
}
}
}
// Or reject the call:
// client.setCallInviteListener { callId, from, channelType ->
// client.reject(callId) {
// it?.let {err ->
// println("Error in rejecting call: $err")
// }
// }
// }
// Invite Cancelled
client.setCallInviteCancelListener { callId, reason ->
when (reason) {
VoiceInviteCancelReason.RemoteCancel -> println("Call Invite for call $callId was cancelled by the caller")
VoiceInviteCancelReason.AnsweredElsewhere -> println("Call Invite for call $callId was answered on another device")
VoiceInviteCancelReason.RejectedElsewhere -> println("Call Invite for call $callId was rejected on another device")
VoiceInviteCancelReason.RemoteTimeout -> println("Call Invite for call $callId timed out")
}
}
// Answer Call
client.setCallInviteListener((callId, from, channelType) -> {
System.out.println("Incoming Call from " + from + " by channel " + channelType);
client.answer(callId, it -> {
if (it != null) {
// Handle Error in answering call
System.out.println("Error in answering call: " + it);
}
return null;
});
return null;
});
// Or reject the call:
// client.setCallInviteListener((callId, from, channelType) -> {
// client.reject(callId, it -> {
// if (it != null) {
// System.out.println("Error in rejecting call: " + it);
// }
// return null;
// });
// return null;
// });
// Invite Cancelled
client.setCallInviteCancelListener((callId, reason) -> {
switch (reason) {
case RemoteCancel:
System.out.println("Call Invite for call " + callId + " was cancelled by the caller");
break;
case AnsweredElsewhere:
System.out.println("Call Invite for call " + callId + " was answered on another device");
break;
case RejectedElsewhere:
System.out.println("Call Invite for call " + callId + " was rejected on another device");
break;
case RemoteTimeout:
System.out.println("Call Invite for call " + callId + " timed out");
break;
}
return null;
});
class inboundCallExampleDelegate: NSObject, VGVoiceClientDelegate {
func voiceClient(_ client: VGVoiceClient, didReceiveInviteForCall callId: VGCallId, from caller: String, with type: VGVoiceChannelType) {
print(caller)
// Answer Invite
client.answer(callId) { error in
guard error != nil else {
print("Failed to answer \(error?.localizedDescription ?? "")")
return
}
}
// Or reject the invite:
// client.reject(callId) { error in
// guard error != nil else {
// print("Failed to reject \(error?.localizedDescription ?? "")")
// return
// }
// }
}
func voiceClient(_ client: VGVoiceClient, didReceiveInviteCancelForCall callId: VGCallId, with reason: VGVoiceInviteCancelReason) {
guard callId == "currentCallId" else {
return
}
switch reason {
case .answeredElsewhere:
print("Call answered elsewhere")
case .remoteCancel:
print("Call cancelled by remote")
case .rejectedElsewhere:
print("Call rejected elsewhere")
case .remoteTimeout:
print("Call timed out")
default:
print("reason unknown")
}
}
func client(_ client: VGBaseClient, didReceiveSessionErrorWith reason: VGSessionErrorReason) {}
func voiceClient(_ client: VGVoiceClient, didReceiveHangupForCall callId: VGCallId, withQuality callQuality: VGRTCQuality, reason: VGHangupReason) {}
}
- (void) voiceClient:(VGVoiceClient *)client didReceiveInviteForCall:(VGCallId)callId from:(NSString *)caller withChannelType:(VGVoiceChannelType)type {
NSLog(@"Caller: %@", caller);
// Answer Invite
[client answer:callId callback:^(NSError * _Nullable error) {
if(error){
NSLog(@"Failed to answer: %@", error);
}
}];
// Or reject the invite:
// [client reject:callId callback:^(NSError * _Nullable error) {
// if(error){
// NSLog(@"Failed to reject: %@", error);
// }
// }];
}
- (void) voiceClient:(VGVoiceClient *)client didReceiveInviteCancelForCall:(VGCallId)callId withReason:(VGVoiceInviteCancelReason)reason {
if(![callId isEqualToString:@"currentCallId"]){
return;
}
switch(reason) {
case VGVoiceInviteCancelReasonAnsweredElsewhere:
NSLog(@"Call answered elsewhere");
break;
case VGVoiceInviteCancelReasonRemoteCancel:
NSLog(@"Call canceled by remote");
break;
case VGVoiceInviteCancelReasonRejectedElsewhere:
NSLog(@"Call rejected elsewhere");
break;
case VGVoiceInviteCancelReasonRemoteTimeout:
NSLog(@"Call timed out");
break;
case VGVoiceInviteCancelReasonUnknown:
NSLog(@"Reason Unknown");
break;
}
}
Invite Cancel Behavior
Invites can be canceled or time out before user action. Your app receives a cancel callback with one of the following reasons:
| Reason | Description |
|---|---|
RemoteCancel |
The caller hung up before the invite was answered. |
RemoteTimeout |
The invite expired on the Vonage API without user action. |
AnsweredElsewhere |
The same user answered the call on another device or client instance. |
RejectedElsewhere |
The same user rejected the call on another device or client instance. |
UX Guidance
- Dismiss incoming call UI immediately when any cancel reason arrives.
- Do not attempt to call
answer()orreject()after receiving a cancel event — the invite is no longer valid. - For multi-device scenarios (
AnsweredElsewhere/RejectedElsewhere), consider showing a brief toast or visual indicator so the user understands why the ringing stopped.
Handling Errors
answer() and reject() can fail if the session has expired, the invite is no longer valid (already cancelled), or there is a network issue. Always handle the error callback to update your UI accordingly:
try {
await client.answer(callId);
showActiveCallUI();
} catch (error) {
console.error("Answer failed:", error.type, error.message);
dismissIncomingCallUI();
}
try {
await client.reject(callId);
} catch (error) {
console.error("Reject failed:", error.type, error.message);
} finally {
dismissIncomingCallUI();
}
client.answer(callId) { error ->
if (error != null) {
// Answer failed — dismiss call UI and inform the user
println("Answer failed: ${error.message}")
// Common causes:
// - Session expired (re-create session and retry)
// - Invite already cancelled by caller
dismissIncomingCallUI()
return@answer
}
// Success — transition to active call UI
showActiveCallUI()
}
client.reject(callId) { error ->
if (error != null) {
// Reject failed — the invite may have already been cancelled
println("Reject failed: ${error.message}")
}
// In both success and failure, dismiss the incoming call UI
dismissIncomingCallUI()
}
client.answer(callId) { error in
if let error = error {
print("Answer failed: \(error.localizedDescription)")
self.dismissIncomingCallUI()
return
}
self.showActiveCallUI()
}
client.reject(callId) { error in
if let error = error {
print("Reject failed: \(error.localizedDescription)")
}
self.dismissIncomingCallUI()
}
If answer() fails due to an expired session, re-create the session with a fresh JWT and retry the answer. If the invite has already been cancelled (caller hung up), there is no recovery — dismiss the UI and wait for the next invite.
Further Reading
- NCCO reference — full documentation for the JSON structure your answer webhook returns