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:

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.

NexmoIncomingCallListener incomingCallListener = new NexmoIncomingCallListener() {
    @Override
    public void onIncomingCall(NexmoCall call) {
        ...
    }
};

nexmoCall.addIncomingCallListener(incomingCallListener);

Remove the listener when needed:

nexmoCall.removeIncomingCallListener(incomingCallListener);

val incomingCallListener = NexmoIncomingCallListener { call ->
    currentCall = call
    startActivity(Intent(this@MainActivity, IncomingCallActivity::class.java))
}

NexmoClient.get().addIncomingCallListener(incomingCallListener)

Remove the listener when needed:

NexmoClient.get().removeIncomingCallListeners()

Add NXMClientDelegate as an extension to a ViewController or similar:

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:

@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

call.answer();
nexmoCall.answer(new NexmoRequestListener<NexmoCall>(){...})
nexmoCall?.answer(object : NexmoRequestListener<NexmoCall> {...})

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

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::

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

Reject

call.reject();

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

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::

[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:

call.hangUp();
nexmoCall.hangup(new NexmoRequestListener<NexmoCall>(){...})
nexmoCall?.hangup(object : NexmoRequestListener<NexmoCall> {...})
call.hangup()
[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:

application.on("call:status:changed",(call) => {
    ...
});
NexmoCallEventListener callEventListener = ... ;

nexmoCall.addCallEventListener(callEventListener);

Remove the listener when needed:

nexmoCall.removeCallEventListener(callEventListener);
var callEventListener = NexmoCallEventListener(...)

nexmoCall?.addCallEventListener(callEventListener)

Remove the listener when needed:

nexmoCall?.removeCallEventListener(callEventListener)

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

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

ViewController will now have to conform to NXMCallDelegate:

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:

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

ViewController will now have to conform to NXMCallDelegate:

@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