Sample VoIP Calling App in Android

DeveloperSpace
Nerd For Tech
Published in
5 min readApr 12, 2021

--

This article describes the designing & implementation of a sample VoIP calling app using CometChat Pro. CometChat Pro is Messaging & Call SDK that provides enormous features for developers to integrate chat. Using CometChat Pro UI Kit, developers can easily integrate real-time messaging & calling support in their app. By the end of this article, you will create a fully functioning Sample VoIP Calling App.

Key Components & Libraries Used

CometChat Android UI Kit — An ready to use UI Kit library which will help us to develop real-time messaging & call support in our sample app within a few minutes.

Firebase — To receive an event when the app is not in the foreground we will use Firebase Push Notification.

ConnectionService — An abstract class that will help you to build VoIP Calls in a sample app. It uses a built-in System UI to manage a call which is also called System Managed. If you wish to use your own UI to manage calls you can use Self-Managed Service.

Prerequisites

Before diving into more details please ensure that you have checked the below points.
— Android studio installed in your system.
— Android Device or Emulator to run your app.

// Clone this repositorygit clone https://github.com/developerspace-samples/VoIP-Call-Sample.git
  • It’s necessary to have CometChat Account to run the app.

You can create your account in CometChat and replace the credentials with yours.

Also, please make sure to setup Push Notification Extension in CometChat.
Please click here to know How to setup Push Notification in CometChat.

  • Next step is to setup Firebase Account and create a new project. Once the project is created, setup an Android App in the Firebase Project and add google-services.json file in your “app” folder

Click here to know How to setup Firebase in Android App

  • Once the above mentioned configurations are done you can open the project in Android Studio. You will find the project contains two packages ‘app’ which is our sample app and ‘uiKit’ is a library provided by CometChat.
  • In ‘app’ you will find the ‘utils’ package under which we have files to handle Push Notification and VoIP calls.

PushNotificationService

It extends FirebaseMessageService which is used to handle Push Notification received from Firebase. We are using it to notify the message sent to a user when they are not active. We are also handling incoming & outgoing calls through it.

onMessageReceived() : This method gets triggered whenever you receive a push notification from firebase.

CometChatHelper.processMessage() : This method is provided by CometChat SDK to get BaseMessage from “data” received as JSON Object in Remote Message.

BaseMessage is base model of message in CometChat
https://docs.cometchat.io/android/v2.0/javadocs/com/cometchat/pro/models/BaseMessage.html

initiateCallService(call : Call) : This method is used to initiate a call through ConnectionService whenever a Call type message is received.

Call is a category of message used in CometChat SDK. https://docs.cometchat.io/android/v2.0/javadocs/com/cometchat/pro/core/Call.html

showMessageNotification(baseMessage: BaseMessage, title: String, alert: String) : This method is used to display and handle the notification for the message when is being received by the particular user.

Click here to get full code of PushNotificationService.kt

CallConnectionService

CallConnectionService is custom connectionservice class which is binded with the app to handle incoming & outgoing calls. You will find onCreateIncomingConnection(), onCreateOutgoingConnection() methods in it.

onCreateIncomingConnection() : This method get triggered whenever telecomManager.addNewIncomingCall() get success on receiving a call.

onCreateIncomingConnectionFailed() : This method get triggered whenever telecomManager.addNewIncomingCall() fails to receive a call.

onCreateOutgoingConnection() : This method get triggered whenever telecomManager.placeCall() get successfully executed to place outgoing call.

onCreateOutgoingConnectionFailed() : This method get triggered whenever telecomManager.placeCall() get failed to place an outgoing call.

Currently, we are using CALL_PROVIDER as Capability of ConnectionService. So you can see your system's native calling UI for incoming & outgoing calls.
You can change it SELF_MANAGED if you wan’t to show your own custom UI for incoming & outgoing calls.

Click here to get full code of CallConnectionService.kt

CallConnection

It is a Connection class that is used to initiate a connection when a user receives an incoming or outgoing call. It usually helps the ConnectionService class to handle the connection requests and their callbacks. It includes certain methods like onAnswer(), onReject(), onHold(), onDestroy(),etc. Whenever the user receives an incoming call and if the user accepts the call then onAnswer() will be triggered. Similarly, if user rejects the call then onReject() is triggered.

onAnswer() : Whenever this method gets triggered, we use CometChat.acceptCall() which will start the CometChat Call Service and start the call session.

onReject() : Whenever this method gets triggered, we use CometChat.rejectCall() which will reject the CometChat Call Service and end the call session.

onDisconnect() : This method gets triggered whenever the initiator of a call ends the calls before the receiver accepts the call. This will ends the call session and make it a Missed Call.

Click here to check full code of CallConnection.kt

CallHandler

It is a class that is used to handle and integrate ConnectionService in your app. Below are its methods that are used in your app.

startIncomingCall(Call call): This method is used whenever you need to start an incoming call. In this method you will find telecomManager.addNewIncomingCall(). This method triggers the connection service and shows the System Incoming Call UI to handle incoming calls.

startOutgoingCall(Call call): This method is used to start ongoing call i.e whenever a user tries to initiate a call. In this method, you will find telecomManager.placeCall(). This method places VoIP calls on behalf of your app.

Click here to check full code of CallHandler.kt

--

--