In this tutorial, we will explain the steps to build an Android Chat App using MirrorFly SDKs. We’ll cover the basics of building an app, integration of the SDKs, enabling chat features. Also, we will explain how to connect your chat app to the MirrorFly server, to implement direct messaging functionality.
Now, let’s quickly check on the requirements to build the app:
This Kotlin tutorial was built with the below requirements,
The initial setup to build your existing chat app using kotlin involves the below steps,
Now, the first initialization process begins with installation, wherein the task of chat SDK implementation is performed at the beginning.
Let's understand this in detail.
Obtaining a license key for your MirrorFly application is one of the utmost steps to be done before moving ahead with integration of MirrorFly chat SDK.
In other words, it is necessary to have an authentication check as to whether the SDKs are authenticated for further processing or not by the MirrorFly server.
So, to get the license key you need to create a MirrorFly account.
You need to follow the below steps to create your MirrorFly console account,
Step 1 : First you have to create an account by signing into the MirrorFly Console page using Registration Page
Step 2 : The registration process requires your basic details such as your name, organization details, contact number, and work email, etc., to get enrolled.
Step 3 : Great, You get your MirrorFly account! Now you are free to explore every loop and corner of your MirrorFly account to get any required information.
Step 4 : However, this lets you get access to the license key. For this, you can simply click on the ‘Overview page’ where you have your license key available, downloadable Android SDK file to proceed with further integration process.
Step 5 : Copy the license key under the "Application info" section in the "Overview" page and begin with the installation process.
To begin with the installation process, the file has to be imported into the project with the support of Kotlin technology. This is carried out using some of the below steps,
Step 1 : To download the latest AAR files from MirrorFly's Android SDKs, you need to first click on the Android SDK.
Step 2 : This will take you to the 'Download button,' available at the top of the guided documentation.
Step 3 : Click on this ‘downloadable button’ and get into the zip folders, now extract the needed AAR files from the zip folder.
Step 1 : After completing the download of AAR files, it’s time to create a new project or open an existing one in Android Studio.
Step 2 : Add the below given libraries in the app/libs folder in the project.
Step 3 : Now, add the below given code in the app/build.gradle file.
plugins {
...
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
packagingOptions {
exclude 'META-INF/AL2.0'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LGPL2.1'
exclude("META-INF/*.kotlin_module")
}
}
Step 4 : Then add the below given dependencies in the app/build.gradle.file.
dependencies {
... // your app dependencies
implementation files('libs/appbase.aar')
implementation files('libs/flycommons.aar')
implementation files('libs/flynetwork.aar')
implementation files('libs/flydatabase.aar')
implementation files('libs/videocompression.aar')
implementation files('libs/xmpp.aar')
}
Step 5 : Following this, add the dependencies given below that are required by the SDK in the app/build.gradle file.
dependencies {
... // your app dependencies
configurations {
all {
exclude group: 'org.json', module: 'json'
exclude group: 'xpp3', module: 'xpp3'
}
}
//For lifecycle listener
implementation 'android.arch.lifecycle:extensions:1.1.1'
annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'
//For GreenDao
implementation 'de.greenrobot:greendao:2.1.0'
//For gson parsing
implementation 'com.google.code.gson:gson:2.8.1'
//for smack implementation
implementation 'org.igniterealtime.smack:smack-android:4.4.4'
implementation 'org.igniterealtime.smack:smack-tcp:4.4.4'
implementation 'org.igniterealtime.smack:smack-im:4.4.4'
implementation 'org.igniterealtime.smack:smack-extensions:4.4.4'
implementation 'org.igniterealtime.smack:smack-sasl-provided:4.4.4'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.google.android.gms:play-services-location:17.0.0'
//Dagger Dependencies
api 'com.google.dagger:dagger:2.40.5'
kapt 'com.google.dagger:dagger-compiler:2.40.5'
api 'com.google.dagger:dagger-android:2.40.5'
api 'com.google.dagger:dagger-android-support:2.40.5'
kapt 'com.google.dagger:dagger-android-processor:2.40.5'
//coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.8'
//apicalls
implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'com.squareup.okhttp3:okhttp:4.2.0'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
//stetho interceptor
implementation 'com.facebook.stetho:stetho-okhttp3:1.3.1'
//okhttp interceptor
implementation 'com.squareup.okhttp3:logging-interceptor:3.14.3'
//shared preference encryption
implementation 'androidx.security:security-crypto:1.1.0-alpha03'
}
Step 6 : Add the below codes in the gradle.properties file, mainly to avoid imported library conflicts.
android.enableJetifier=true
Step 7 : Once done with the above, open the AndroidManifest.xml and add the below permissions.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
buildTypes {
debug {
buildConfigField 'String', 'SDK_BASE_URL', '"https://api-preprod-sandbox.mirrorfly.com/api/v1/"'
buildConfigField 'String', 'LICENSE', '"xxxxxxxxxxxxxxxxxxxxxxxxx"'
buildConfigField 'String', 'WEB_CHAT_LOGIN', '"https://webchat-preprod-sandbox.mirrorfly.com/"'
buildConfigField "String", "SUPPORT_MAIL", '"contussupport@gmail.com"'
}
}
To start with the initialization process of the SDK, there is a need for some fundamental data before proceeding further. Therefore, the ChatSDK builder class is made into use to provide these necessary data to the available SDK.
Henceforth, you can call upon the below given ChatSDK Builder in the onCreate() method to provide the necessary data for your application class. Let's have a look in detail,
//For chat logging
LogMessage.enableDebugLogging(BuildConfig.DEBUG)
ChatSDK.Builder()
.setDomainBaseUrl(BuildConfig.SDK_BASE_URL)
.setLicenseKey(BuildConfig.LICENSE)
.setIsTrialLicenceKey(true)
.build()
For every activity to take place in the messaging app, there is a need for registration, for which you must undergo an authentication process. Thus, to register a new user in the Sandbox Live mode you have to call upon the below method based on the provided setIsTrialLicenseKey.
Here, while registration you must ensure that the method has been called only once before closing the session. However, in this context you must call upon the below registerUser method with the acceptance of FCM_TOKEN to process further.
FlyCore.registerUser(USER_IDENTIFIER) { isSuccess, throwable, data ->
if(isSuccess) {
val isNewUser = data["is_new_user"] as Boolean
val responseObject = data.get("data") as JSONObject
// Get Username and password from the object
} else {
// Register user failed print throwable to find the exception details.
}
}
Argument | Type | Description |
---|---|---|
USER_IDENTIFIER | String | A unique Id to Register the User |
FCM_TOKEN | String | A registration token that is generated by FCM SDK for the user's app instance to send message for free |
CALLBACK | FlyCallback | FlyCallback is used as a callback, implemented and expressed as lambda expression for easy reading |
Before moving ahead with any activity, it is necessary to get connected to the chat server. Thus, you have to establish a connection with the server. Here, we need a chat SDK messaging with the below method to initiate the process.
However, the process stores all the details about the connection to work further automatically.
ChatManager.connect(object : ChatConnectionListener {
override fun onConnected() {
// Write your success logic here to navigate Profile Page or
// To Start your one-one chat with your friends
}
override fun onDisconnected() {
// Connection disconnected
//No need implementations
}
override fun onConnectionNotAuthorized() {
// Connection Not authorized
//No need implementations
}
})
Argument | Type | Description |
---|---|---|
CALLBACK | ChatConnectionListener | callback listener for chat connection |
Let's have a look at some of the common terminologies that are used in the Chat SDK with description,
Keyword | Description |
---|---|
userID/userBareID | Unique ID assigned for each user Ex: 12345678 (any alphanumeric). The below characters is not allowed in userId: U+0022 (") U+0026 (&) U+0027 (') U+002F (/) U+003A (:) U+003C (<) U+003E (>) U+0040 (@) userID should follow below specification: https://xmpp.org/extensions/xep-0106.html |
userJid (or) JID | userID+@+domain of the chat server Ex. 12345678@xmpp.chatsystem.dev.contus.us |
groupID/groupBareID | Unique ID assigned for each group Ex: group123456 (any alphanumeric). GroupJID = groupID +@mix.+domain of the chat server Ex: group123456@mix.xmpp.mirrorfly.dev.contus.us |
Once you have made all the connections with the server now it's the time to perform text messaging within your existing kotlin chat app in android.
You can use the below method to send a text message from one user to another,
But, here there is a need to create a unique user id for which you must call the other user's using username method String userJID = FlyUtils.getJid(USERNAME)
FlyMessenger.sendTextMessage(TO_JID, TEXT, listener = object : SendMessageListener {
override fun onResponse(isSuccess: Boolean, chatMessage: ChatMessage?) {
// you will get the message sent success response
}
})
Argument | Type | Description |
---|---|---|
TO_JID/ | String/ | JID of the end user |
TEXT/ | String/ | Indicates the text message that needs to be sent |
REPLY_MESSAGE_ID/ | String/ | Specifies the Message ID of replied message | CALLBACK/ | SendMessageListener/ | callback to observe the action status |
To receive a text message, you can extend the FlyBaseActivity from the SDK within your app using the BaseActivity method. In addition, you will also be able to observe all the incoming messages and other features listeners.
class MainActivity : FlyBaseActivity()
However, here the listeners would be called only when a new message is received from the other users end.
override fun onMessageReceived(message: ChatMessage) {
super.onMessageReceived(message)
// received message object
}
Well, the above are the basic features and functionalities used in any messaging app, but if you are in need of some specific functionalities then our Chat SDK is there to support you with all your needs.
And we’re done! This tutorial took you through the complete process of building a chat app in Kotlin, with the help of MirrorFly Chat SDKs. If you’ve found this guide simple and easy, you may also check out our other features here and experiment with them
If you'd like to get assistance with the SDK installation from our team of experts, write to us and we will get back to you. Until then, happy chat app development!
Drive 1 billion + conversations on any Android, iOS, or Web app with 150+ chat features & 100+ UI components.
Integrate Our Video, Voice & Chat SDKs into any app in < 30 mins
Contact SalesGet Full Access To Our Customizable Video, Voice & Chat SDKs!
Request DemoTrusted by 100+ Clients