Receive a Call

In order to receive an incoming in-app call, you should register and listen to incoming call events:

To receive an incoming in-app call, you should listen to incoming call events:

Copy to Clipboard
application.on("member:call", (member, call) => {
    ...
});

The listener method receives a member object that contains information about the caller and a call object, that lets you interact with the call in progress. With the later, you’ll be able to perform methods such as answer, reject and hang up.

Copy to Clipboard
val incomingCallListener = NexmoIncomingCallListener {
    Log.d("TAG", "Incoming call $it")
}

val client = NexmoClient.get()
client.addIncomingCallListener(incomingCallListener)

Remove the listener when needed:

Copy to Clipboard
client.removeIncomingCallListeners()
Copy to Clipboard
NexmoIncomingCallListener incomingCallListener = new NexmoIncomingCallListener() {
    @Override
    public void onIncomingCall(NexmoCall call) {
        Log.d("TAG", "Incoming call " + call);
    }
};

val client = NexmoClient.get()
client.addIncomingCallListener(incomingCallListener);

Remove the listener when needed:

Copy to Clipboard
client.removeIncomingCallListeners();

Add NXMClientDelegate as an extension to a ViewController or similar:

Copy to Clipboard
extension ViewController: NXMClientDelegate {

    func client(_ client: NXMClient, didChange status: NXMConnectionStatus, reason: NXMConnectionStatusReason) {
        // handle connection status changed - eg: logout
        ...
    }

    func client(_ client: NXMClient, didReceiveError error: Error) {
        print("connection error: \(error.localizedDescription)")
        // handle client connection failure
        ...
    }

    func client(_ client: NXMClient, didReceive call: NXMCall) {
        print("Incoming Call: \(call)")
        // handle incoming call
        ...
    }

}

Have a ViewController, or similar, conform to NXMClientDelegate:

Copy to Clipboard
@interface ViewController () <NXMClientDelegate>
...
@end

@implementation ViewController
...

//MARK:- NXMClientDelegate


- (void)client:(nonnull NXMClient *)client didChangeConnectionStatus:(NXMConnectionStatus)status 
        reason:(NXMConnectionStatusReason)reason {
    // handle connection status changed - eg: logout
    ...
}

- (void)client:(nonnull NXMClient *)client didReceiveError:(nonnull NSError *)error {
    NSLog(@"connection error: %@", [error localizedDescription]);
    // handle client connection failure
    ...
}

- (void)client:(nonnull NXMClient *)client didReceiveCall:(nonnull NXMCall *)call {
    NSLog(@"Incoming Call: %@", call);
    // handle incoming call
    ...
}
@end

Then, you’ll be able to perform methods such as answer, reject and hang up.

Answer

Copy to Clipboard
call.answer();
Copy to Clipboard
val incomingCallListener = NexmoIncomingCallListener {
    Log.d("TAG", "Incoming call $it")

    it.answer(answerCallListener)
}

val answerCallListener = object: NexmoRequestListener<NexmoCall> {
    override fun onSuccess(nexmoCall: NexmoCall?) {
        Log.d("TAG", "Call answered: $nexmoCall")
    }

    override fun onError(apiError: NexmoApiError) {
        Log.d("TAG", "Error: Unable to answer incoming call ${apiError.message}")
    }
}

val client = NexmoClient.get()
client.addIncomingCallListener(incomingCallListener)
Copy to Clipboard
NexmoIncomingCallListener incomingCallListener = new NexmoIncomingCallListener() {
    @Override
    public void onIncomingCall(NexmoCall call) {
        Log.d("TAG", "Incoming call " + call);

        call.answer(answerCallListener);
    }
};

NexmoRequestListener<NexmoCall> answerCallListener = new NexmoRequestListener<NexmoCall>() {
    @Override
    public void onSuccess(@Nullable NexmoCall nexmoCall) {
        Log.d("TAG", "Call answered: " + nexmoCall);
    }

    @Override
    public void onError(@NonNull NexmoApiError apiError) {
        Log.d("TAG", "Error: Unable to answer incoming call " + apiError.getMessage());
    }
};

NexmoClient client = NexmoClient.get();
client.addIncomingCallListener(incomingCallListener);

Using the call object received in client(_:didReceive:):

Copy to Clipboard
call.answer { [weak self] error in
    if let error = error {
        print("error answering call: \(error.localizedDescription)")
        // handle call answer failure
        ...
        return
    }
    // handle call answer success
    ...
}

Using the call object received in client:didReceiveCall::

Copy to Clipboard
[call answer:^(NSError * _Nullable error) {
    if (error) {
        NSLog(@"error answering call: %@", error.localizedDescription);
        // handle call answer failure
        ...
        return;
    }
    // handle call answer success
    ...
}];

Reject

Copy to Clipboard
call.reject();

Using the call object received in client(_:didReceive:):

Copy to Clipboard
call.reject { [weak self] error in
    if let error = error {
        print("error declining call: \(error.localizedDescription)")
        // handle call decline failure
        ...
        return
    }
    // handle call decline success
    ...
}

Using the call object received in client:didReceiveCall::

Copy to Clipboard
[call reject:^(NSError * _Nullable error) {
    if (error) {
        NSLog(@"error answering call: %@", error.localizedDescription);
        // handle call decline failure
        ...
        return;
    }
    // handle call decline success
    ...
}];

Hang Up

For an on-going call:

Copy to Clipboard
call.hangUp();
Copy to Clipboard
val hangupCallListener = object: NexmoRequestListener<NexmoCall> {
    override fun onSuccess(nexmoCall: NexmoCall?) {
        Log.d("TAG", "Call hangup: $nexmoCall")
    }

    override fun onError(apiError: NexmoApiError) {
        Log.d("TAG", "Error: Unable to hangup call ${apiError.message}")
    }
}

nexmoCall.hangup(hangupCallListener)
Copy to Clipboard
NexmoRequestListener<NexmoCall> hangupCallListener = new NexmoRequestListener<NexmoCall>() {
    @Override
    public void onSuccess(@Nullable NexmoCall nexmoCall) {
        Log.d("TAG", "Call hangup: " + nexmoCall);
    }

    @Override
    public void onError(@NonNull NexmoApiError apiError) {
        Log.d("TAG", "Error: Unable to hangup call " + apiError.getMessage());
    }
};

nexmoCall.hangup(hangupCallListener);
Copy to Clipboard
call.hangup()
Copy to Clipboard
[call hangup];

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 call events.

To see updates on the state of the call and its members:

Copy to Clipboard
application.on("call:status:changed",(call) => {
    ...
});
Copy to Clipboard
val callEventListener = object : NexmoCallEventListener {
    // ...
}

nexmoCall.addCallEventListener(callEventListener)

Remove the listener when needed:

Copy to Clipboard
nexmoCall?.removeCallEventListener(callEventListener)
Copy to Clipboard
NexmoCallEventListener callEventListener = new NexmoCallEventListener() {
   // ...
}

nexmoCall.addCallEventListener(callEventListener);

Remove the listener when needed:

Copy to Clipboard
nexmoCall.removeCallEventListener(callEventListener);

Add the current ViewController, or similar, as a delegate for the call before answering:

Copy to Clipboard
call.setDelegate(self)
call.answer { [weak self] error in
    ...
}

ViewController will now have to conform to NXMCallDelegate:

Copy to Clipboard
extension ViewController: NXMCallDelegate {

    func call(_ call: NXMCall, didUpdate callMember: NXMCallMember, with status: NXMCallMemberStatus) {
        // handle call status updates
        ...
    }

    func call(_ call: NXMCall, didUpdate callMember: NXMCallMember, isMuted muted: Bool) {
        // handle call member muting updates
        ...
    }

    func call(_ call: NXMCall, didReceive error: Error) {
        print("call error: \(error.localizedDescription)")
        // handle call errors
        ...
    }

}

Add the current ViewController, or similar, as a delegate for the call before answering:

Copy to Clipboard
[call setDelegate:self];
[call answer:^(NSError * _Nullable error) {
    ...
}];

ViewController will now have to conform to NXMCallDelegate:

Copy to Clipboard
@interface ViewController () <NXMCallDelegate>
...
@end

@implementation ViewController
...

//MARK:- NXMCallDelegate

- (void)call:(nonnull NXMCall *)call didUpdate:(nonnull NXMCallMember *)callMember withStatus:(NXMCallMemberStatus)status {
    // handle call status updates
    ...
}

- (void)call:(nonnull NXMCall *)call didUpdate:(nonnull NXMCallMember *)callMember isMuted:(BOOL)muted {
    // handle call member muting updates
    ...
}

- (void)call:(nonnull NXMCall *)call didReceive:(nonnull NSError *)error {
    NSLog(@"call error: %@", [error localizedDescription]);
    // handle call errors
    ...
}

@end