How to Make and Receive In App calls with the Nexmo Client SDK on Android

In this tutorial you learn how to use the Nexmo Client SDK for Android, in order to perform an in-app (IP to IP) voice call.

You create a simple app that can make a call and receive a call.

The app will have two buttons, which log in different users: Jane or Joe. After logging in, Jane and Joe are able to place a call and perform actions such as answer, reject or hangup.

Prerequisites

  1. Create a Nexmo Application.

  2. Create two users on that Nexmo Application: one with the name Jane and the other with the name Joe.

The starter project

Clone or download the GitHub repository on either Kotlin orJava. On that repository you'll find two apps:

  • GetStartedCalls-Start - if you want to follow along and add the code with this tutorials
  • GetStartedCalls-Complete - if you want to look at the final result

Open the NexmoHelper class.

  1. Make sure that enabledFeatures array contains Features.IN_APP_to_IN_APP. You can remove the rest, if you haven't completed their tutorials yet.

  2. Replace the user IDs and tokens:

String USER_ID_JANE = "USR-XXX"; //TODO: replace with the UserId you generated for Jane
String USER_ID_JOE = "USR-XXX"; //TODO: replace with the UserId you generated for Joe
String JWT_JANE = "PLACEHOLDER";//TODO: replace with the JWT you generated for Jane
String JWT_JOE = "PLACEHOLDER"; //TODO: replace with the JWT you generated for Joe
val USER_ID_JANE = "USR-XXX"; //TODO("replace with the UserId you generated for Jane")
val USER_ID_JOE = "USR-XXX"; //TODO("replace with the UserId you generated for Joe")
val JWT_JANE = "PLACEHOLDER"; //TODO("replace with the JWT you generated for Jane")
val JWT_JOE = "PLACEHOLDER"; //TODO("replace with the JWT you generated for Joe")

Login

To start using the Nexmo Client SDK you need to log in to NexmoClient, using a JWT user token.

In production apps, your server would authenticate the user, and would return a correctly configured JWT to your app.

For testing and getting started purposes, you can use the Nexmo CLI to generate JWTs.

Open LoginActivity. It already has two button handlers: onLoginJaneClick(...) and onLoginJoeClick(...). Each calls the loginToSdk(...) method, with a different parameter - the corresponding jwt.

When the login is successful, the logged in NexmoUser returns. For convenience, save a reference to NexmoUser on NexmoHelper, and then, start MainActivity.

Complete the loginToSdk() method implementation:

void loginToSdk(String token) {
    NexmoClient.get().login(token, new NexmoRequestListener<NexmoUser>() {

        @Override
        public void onError(NexmoApiError nexmoApiError) {}

        @Override
        public void onSuccess(NexmoUser user) {
            NexmoHelper.user = user;

            Intent intent = new Intent(getBaseContext(), MainActivity.class);
            startActivity(intent);
            finish();
        }
    });
}
fun loginToSdk(token: String) {
        NexmoClient.get().login(token, object : NexmoRequestListener<NexmoUser> {

            override fun onError(nexmoApiError: NexmoApiError) {
                notifyError(nexmoApiError)
            }

            override fun onSuccess(user: NexmoUser) {
                currentUser = user

                val intent = Intent(baseContext, MainActivity::class.java)
                startActivity(intent)
                finish()
            }
        })
    }
}

At this point you should already be able to run the app and see that you can login successfully with the SDK.

Start a Call

You can now make a simple In-App call. If you logged in as Jane, you can call Joe, and if you logged in as Joe you can call Jane.

Open MainActivity and complete the prepared onInAppCallClick() handler:

public void onInAppCallClick(View view) {
    List<String> callee = new ArrayList<>();
    callee.add(getOtherUserId());

    NexmoClient.get().call(callee, NexmoCallHandler.IN_APP, callListener);
}

String getOtherUserId() {
    return NexmoHelper.user.getName().equals(NexmoHelper.USER_NAME_JANE) ? NexmoHelper.USER_ID_JOE : NexmoHelper.USER_ID_JANE;
}
fun onInAppCallClick(view: View) {
        val callee = listOf(otherUserName)
        NexmoClient.get().call(callee, NexmoCallHandler.IN_APP, callListener)
}

val otherUserName: String
    get() = if (user?.name == USER_NAME_JANE) USER_NAME_JOE else USER_NAME_JANE

When the call creation is successful, save the NexmoCall on NexmoHelper, for convenience, and start OnCallActivity:

NexmoRequestListener<NexmoCall> callListener = new NexmoRequestListener<NexmoCall>() {
    @Override
    public void onError(NexmoApiError nexmoApiError) { }

    @Override
    public void onSuccess(NexmoCall call) {
        NexmoHelper.currentCall = call;

        Intent intent = new Intent(MainActivity.this, OnCallActivity.class);
        startActivity(intent);
    }
};
var callListener = object: NexmoRequestListener<NexmoCall> {
    override fun onError(nexmoApiError: NexmoApiError) {
        notifyError(nexmoApiError)
    }

    override fun onSuccess(call: NexmoCall) {
        currentCall = call

        val intent = Intent(this@MainActivity, OnCallActivity::class.java)
        startActivity(intent)
    }
}

You can also start a call with customized logic using an NCCO, by choosing NexmoCallHandler.SERVER as the CallHandler:

NexmoCient.get().call(callees, NexmoCallHandler.SERVER, listener)
NexmoCient.get().call(callees, NexmoCallHandler.SERVER, listener)

This allows you to start a PSTN phone call, by adding a phone number to the callees list.

Register for incoming events

When Jane calls Joe, Joe should be notified. Joe can then answer the call. Joe should register to incoming events, and implement onIncomingCall(). Whenever Joe is called, onIncomingCall() is invoked with the incoming Call object.

For simplicity in this example, you will accept incoming calls only on MainActivity. Open MainActivity and create the NexmoIncomingCallListener to save the reference to the incoming call on NexmoHelper, and start IncomingCallActivity:

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

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

You need to register and unregister the listener in onCreate() and onDestroy():

@Override
protected void onStart() {
    super.onStart()
    NexmoClient.get().addIncomingCallListener(incomingCallListener);
}

@Override
protected void onStop() {
    NexmoClient.get().removeIncomingCallListeners();
    super.onStop();
}
override fun onStart() {
    super.onStart()
    NexmoClient.get().addIncomingCallListener(incomingCallListener)
}

override fun onStop() {
    NexmoClient.get().removeIncomingCallListeners()
    super.onStop()
}

Answer a call

Once a incoming call is received, it can be answered. Open IncomingCallActivity, and complete the prepared onAnswer() button handler, to start OnCallActivity after a successful answer:

public void onAnswer(View view) {
    NexmoHelper.currentCall.answer(new NexmoRequestListener<NexmoCall>() {
        @Override
        public void onError(NexmoApiError nexmoApiError) { }

        @Override
        public void onSuccess(NexmoCall call) {
            startActivity(new Intent(IncomingCallActivity.this, OnCallActivity.class));
            finish();
        }
    });
}
fun onAnswer(view: View) {
    currentCall?.answer(object : NexmoRequestListener<NexmoCall> {
        override fun onError(nexmoApiError: NexmoApiError) {}

        override fun onSuccess(call: NexmoCall) {
            startActivity(Intent(this@IncomingCallActivity, OnCallActivity::class.java))
            finish()
        }
    })
}

Hangup

The onHangup() handler allows to reject the call. Complete the implementation in IncomingCallActivity to finish the activity:

public void onHangup(View view) {
    NexmoHelper.currentCall.hangup(new NexmoRequestListener<NexmoCall>() {
        @Override
        public void onError(NexmoApiError nexmoApiError) { }

        @Override
        public void onSuccess(NexmoCall call) {
            finish();
        }
    });
}
currentCall?.hangup(object : NexmoRequestListener<NexmoCall> {
    override fun onError(nexmoApiError: NexmoApiError) {}

    override fun onSuccess(call: NexmoCall) {
        startActivity(Intent(this@IncomingCallActivity, OnCallActivity::class.java))
        finish()
    }
})

Register to call status

To be aware of the call status, for example, if one of the members answers or hangs up, you should register to CallEvents. The FinishOnCallEnd is a NexmoCallEventListener that finishes the current activity if the call is completed or canceled.

Register to its instance, to address the use cases mentioned previously.

On both OnCallActivity and IncomingCallActivity, add:

NexmoCallEventListener callEventListener = new FinishOnCallEnd(this);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    NexmoHelper.currentCall.addCallEventListener(callEventListener);
}


@Override
protected void onDestroy() {
    NexmoHelper.currentCall.removeCallEventListener(callEventListener);
    super.onDestroy();
}
var callEventListener = FinishOnCallEnd(this)

override fun onCreate(savedInstanceState: Bundle?) {
    currentCall?.addCallEventListener(callEventListener)
}


override fun onDestroy() {
    currentCall?.removeCallEventListener(callEventListener)
    super.onDestroy()
}

Handle permissions

For devices running Android 6.0 (API level 23) and higher, creation and operation of calls requires requesting runtime permissions. To simplify the implementation in this tutorial, BaseActivity checks the permissions in every Activity's onStart() and onStop().

To read more about the permissions required, see the setup tutorial.

Conclusion

Congratulations! You have implemented your first In-App Voice application with Nexmo Client SDK for Android.

You can now run the app on two devices, log in as two different users, and start an in-app voice call between the users.