这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。

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.

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

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

Remove the listener when needed:

client.removeIncomingCallListeners()
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:

client.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();
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)
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:):

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();
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)
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);
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) => {
    ...
});
val callEventListener = object : NexmoCallEventListener {
    // ...
}

nexmoCall.addCallEventListener(callEventListener)

Remove the listener when needed:

nexmoCall?.removeCallEventListener(callEventListener)
NexmoCallEventListener callEventListener = new 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