Getting started with Sinch In-app Calling for Android SDK

This guide shows you how to get started integrating your Android application with the In-app Calling SDK. Because this is just a getting started guide, we only covers basic steps how to sign in to the In-app Calling SDK and how to make and receive audio calls.

Notes:

This guide is meant to be used side by side with our samples available in the SDK
Code snippets shown in the guide are part of 'sinch-rtc-sample-push' sample available on our download page inside the SDK archive. Please follow along with the samples open in your IDE.

For more complex examples and documentation, check inside the SDK package file or look at our tutorials.

Prerequisites

  • Android Studio and Android SDK tools available here.
  • In-app Calling SDK for Android.
  • For convenience 2 physical Android devices (however 1 physical and 1 emulator is enough for testing purposes)

Create Android Studio project

Create a new Android Studio project using the 'Empty activity' template.

as1

Notes:
  • You can use Kotlin as your development language in both Java and Kotlin versions of the SDK.
  • The name of your application doesn't need to correspond to the one provided in the Sinch Dashboard.

Add Sinch Voice and Video SDK to your Android application

  1. In your web browser, go to Sinch SDKs Download page.
  2. Find the Android SDK for Java, download the zip file and extract it.
  3. Along with the library itself, the package also contains documentation and sample applications. For the purpose of this tutorial only the aar file is needed. Locate it inside the 'libs' folder and copy it to your clipboard.
  4. Paste it to <your android application name>/app/libs directory.

    as2

  5. Edit your app's build.gradle file located under app/build.gradle to include the following: app/build.gradle
Copy
Copied
repositories {
    flatDir {
        dirs 'libs'
        }
    }

dependencies {
    implementation(name:'sinch-android-rtc', version:'+', ext:'aar')
    ///
    }

You should see a message that gradle files have changed. Click Sync now and wait for Android Studio to adopt your changes.

Interacting with Voice and Video SDK

The SinchClient object is the main SDK entry point. Once created it's used to provide various SDKs features such as video, audio or PTSN calls. We recommend to initiate SinchClient once and retain its instance during the whole lifetime of the running application. That's why instead of placing it inside Activity or Fragment classes we recommend to put it inside a Service component.
  1. Create a new Kotlin class that extends Service component (don't forget to define the service in your AndroidManifest.xml file).
  2. Add SinchClient instance as a member variable.
  3. Create a function that builds the client.

The result of these steps is demonstrated below:

Copy
Copied
    class SinchService : Service() {

        companion object {
            private const val APP_KEY = "enter-application-key"
            private const val APP_SECRET = "enter-application-secret"
            private const val ENVIRONMENT = "ocra.api.sinch.com"
        }

        private var sinchClient: SinchClient? = null

        private fun createClient(username: String) {
            sinchClient = SinchClient.builder().context(applicationContext)
                .userId(username)
                .applicationKey(APP_KEY)
                .environmentHost(ENVIRONMENT)
                .pushConfiguration(
                    PushConfiguration.fcmPushConfigurationBuilder()
                        .senderID(APP_FCM_SENDER_ID)
                        .registrationToken(getFcmRegistrationToken(this).orEmpty()).build()
                )
                .pushNotificationDisplayName("User $username")
                .build()
            sinchClient?.addSinchClientListener(MySinchClientListener())
            sinchClient?.callController?.addCallControllerListener(SinchCallControllerListener())
        }
        ///
    }

To make this example work for you, you need to update some values:

ParameterYour value
APP_KEYYou can find your key on your dashboard.
APP_SECRETYou can find your secret on your dashboard. Your secret is only available to view right after creating your key, so make sure you copy it somewhere safe.
pushConfigurationFCM or HMS push configuration needed by the SDK to send push messages notifying about incoming calls. See push notifications section for more information.

There are a few other elements to notice:

  • The environmentHost parameter is the endpoint of the REST API the SDK is targeting.
  • The UserId parameter is the user identifier to register within your application (the specific value will be provided after clicking Login button in the Android application).
When starting the Sinch client, SinchClientListener's onCredentialsRequired method executes. Inside this callback you must provide a signed (with your application secret) JWT token. In a production application the token should be generated on your backend infrastructure. Most importantly you shouldn't put your app secret value into your Android application source code. More information on that topic can be found in Authentication & Authorization and Authorizing the Client sections.Just for this step-by-step guide purpose we will mimic a backend authentication server behaviour with a helper JWT class that creates the token based on userId and your application credentials locally and then passes it back to Sinch SDK:
Copy
Copied
    override fun onCredentialsRequired(clientRegistration: ClientRegistration) {
        clientRegistration.register(create(APP_KEY, APP_SECRET, settings.username))
    }

Implementation of the JWT class can be found in any of samples provided with the SDK package.

(For example, check out samples/sinch-rtc-sample-push/src/com/sinch/android/rtc/sample/push/JWT.kt)

Communication between views and service

To communicate between your application view layer (activities and fragments) and the Sinch client that lives inside the Service instance, we implement a bound service pattern. Information about what's a bound service and how it works can be found on the official Android SDK documentation website.

  1. Inside SinchService create an inner class that extends Binder.
  2. Add basic functionality that allows the application to start Sinch client for a given username and provide a way to be notified about the initialization result:
    Copy
    Copied
        private var listener: StartFailedListener? = null
    
        interface StartFailedListener {
            fun onFailed(error: SinchError)
            fun onStarted()
        }
    
        inner class SinchServiceInterface : Binder() {
    
            fun startClient() {
                // The username is fetched from settings.
                start()
            }
    
            fun setStartListener(listener: StartFailedListener?) {
                this@SinchService.listener = listener
            }
        }
  3. Override onBind method to return SinchServiceBinder
    Copy
    Copied
        private val sinchServiceInterface: SinchServiceInterface = SinchServiceInterface()
    
        override fun onBind(intent: Intent): IBinder {
            ...
            return sinchServiceInterface
        }

Logging into the application

When the user starts the application, usually they must enter a username that will be passed as userId and used to create a Sinch client. The username they choose will then be used as a callee identifier for making the actual audio call.
  1. Rename MainActivity and activity_main.xml files inside AndroidStudio (that were initially created after setting up the project) to LoginActivity and login.xml. Right click on the filename and choose Refactor -> Rename.
  2. Create a simple layout containg EditText (for entering the username) and login button.
    Copy
    Copied
        <LinearLayout
            android:id="@+id/userNameInput"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:orientation="vertical"
            android:paddingLeft="@dimen/margin_big"
            android:paddingRight="@dimen/margin_big">
    
                <com.google.android.material.textfield.TextInputLayout
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/input_layout_bottom"
                    android:layout_marginTop="@dimen/margin_big"
                    android:hint="@string/enter_your_id"
                    app:startIconDrawable="@drawable/ic_baseline_person_24_black"
                    app:startIconTint="@color/colorPrimaryDark">
    
                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/loginName"
                        android:inputType="text"
                        android:imeOptions="actionDone"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
    
                </com.google.android.material.textfield.TextInputLayout>
    
        </LinearLayout>
    
        <com.google.android.material.button.MaterialButton
            android:id="@+id/loginButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginStart="@dimen/margin_average"
            android:layout_marginEnd="@dimen/margin_average"
            android:layout_marginBottom="@dimen/margin_average"
            android:padding="@dimen/margin_base_plus"
            android:text="@string/login"
            android:textColor="@color/white"
            app:backgroundTint="@color/sinch_green" />
  3. Inside the LoginActivity file first bind to the Sinch client service by calling
    Copy
    Copied
        private fun bindService() {
            val serviceIntent = Intent(this, SinchService::class.java)
            applicationContext.bindService(serviceIntent, this, BIND_AUTO_CREATE)
        }
    Note that as a second argument LoginActivity is passed, meaning it has to implement the Service Connection interface.
  4. Inside onServiceConnected in BaseActivity callback assign provided binder to a local variable for later usage for communicating with the service and assign LoginActivity as a listener to get notifications about success or failure when starting Sinch client.BaseActivity
    Copy
    Copied
        protected var sinchServiceInterface: SinchService.SinchServiceInterface? = null
            private set
    
        override fun onServiceConnected(componentName: ComponentName, iBinder: IBinder) {
            if (SinchService::class.java.name == componentName.className) {
                sinchServiceInterface = iBinder as SinchService.SinchServiceInterface
                onServiceConnected()
            }
        }
        protected open fun onServiceConnected() {
            // for subclasses
        }
    LoginActivity
    Copy
    Copied
        override fun onServiceConnected() {
            if (sinchServiceInterface?.isStarted == true) {
                openPlaceCallActivity()
            } else {
                sinchServiceInterface?.setStartListener(this)
            }
        }
  5. Finally assign the login button a click listener that initiates and starts the Sinch client:
    Copy
    Copied
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            //
            loginButton.apply {
                setOnClickListener { loginClicked() }
            }
        }
    
        private fun loginClicked() {
            val username = loginNameEditText.text.toString()
            sinchServiceInterface?.username = username
            startClientAndOpenPlaceCallActivity()
        }
  6. Return to SinchService implementation. Inside onClientStarted and onClientFailed callbacks of SinchClientListener simply pass the result to the sinchClientInitializationListener.
    Copy
    Copied
        override fun onClientFailed(client: SinchClient, error: SinchError) {
            listener?.onFailed(error)
            ...
        }
    
        override fun onClientStarted(client: SinchClient) {
            listener?.onStarted()
        }
  7. Before launching the application declare 2 permissions inside your AndroidManifest.xml file that are required to start the SinchClient:
    Copy
    Copied
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  8. Run the application, enter a username of your choice and verify your logcat output. You should see that the client was started successfully.

as3

Next steps

Now that your application is created, you can configure that application to make a call.

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.