Skip to main content

Getting Started

The LikeMinds Android SDK enables your app to have Chat features. Here we are going to talk about how you can integrate chat SDK in your app. Follow below steps to add LikeMinds SDK in your Android Project:

Prerequisites

  1. Kotlin Version: Your project's kotlin version should be greater than 1.6.0.
  2. Minimun Android SDK Version: minSdk should be atleast 21.
  3. Dependency Resolution Management: mavenCentral() and jitpack.io should be mentioned in dependencyResolutionManagement.
  4. LikeMinds API Key: Sign up on the LikeMinds Dashboard and obtain an API key for your application, using this.

Step-by-Step Integration

Step 1 - Installation

  1. Fork our repository Chat Repository from Github.
  2. Add the forked Repo as submodule in your project by running the following command.
git submodule add <link-of-the-forked-repo>
git submodule update --init --recursive
info

If the Kotlin version in your project is above 1.6.20 then checkout to branch master-1.8.22 in the submodule.

git checkout master-1.8.22
  1. Include the chatmm project in your project by pasting the following code in your settings.gradle.
include ':chatmm'
project(':chatmm').projectDir = new File(rootDir, '<name-of-the-forked-project>/chatmm/')
  1. If you are not using navigation library already, add the following code in your project level build.gradle.
buildscript {
...
apply from: '<name-of-the-forked-project>/lm-chat-root-dependencies.gradle'
dependencies {
classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0'
}
}
note

If there is any conflict between the the navigation safe args gradle plugin version, go to lm-chat-root-dependencies.gradle and change lm_chat_versions.navigation to your version.

  1. Enable dataBinding in your project (if not done already) by adding the following code in your app level build.gradle.
android {
buildFeatures {
dataBinding = true
}
}
  1. Now sync your project gradle and you will see chatmm module in your project directory.

Step 2 - Setup LikeMinds Chat

Initiate LMChatCore in onCreate() method of the Application class using the following code:

val application = this // instance of the application
val lmChatTheme = LMChatTheme.COMMUNITY_CHAT // chat theme choosen
val lmChatCoreCallback= null // instance of the callback
val lmChatAppearanceRequest = null // instance of the appearance
val domain = "ENTER YOUR DOMAIN NAME" // domain of the app
val enablePushNotifications = false // enable or disable push notifications
val deviceId = null // device id of the user

LMChatCore.setup(
application = application,
theme = lmChatTheme,
lmChatCoreCallback = lmChatCoreCallback,
lmChatAppearanceRequest = lmChatAppearanceRequest,
domain = domain,
enablePushNotifications = enablePushNotifications,
deviceId = deviceId
)
VARIABLETYPEDESCRIPTIONOPTIONAL
applicationApplicationInstance of your application class.
themeLMChatThemeTheme selected for the chat, possible values
- COMMUNITY_CHAT
- NETWORKING_CHAT
- COMMUNITY_HYBRID_CHAT
domainStringYour domain url.
enablePushNotificationsBooleanWhether to enable push notifications or not
deviceIdStringUnique Id of the device, if notifications are enabled
lmChatAppearanceRequestLMChatAppearanceRequestRequest object to set appearance to the Chat.
lmChatCoreCallbackLMChatCoreCallbackCallback for various operations in SDK .

Step 3 - Initiate User Session

You have successfully initiated the LMChatCore. Now, you have to initiate a user session. You can initiate the user session using one of the following two approaches:

1. Client Side Authentication (With API Key)

This approach should be used when you want to manage LikeMinds authentication tokens on frontend. In this case you provide API Key directly to LikeMinds Chat SDK, which will be used to initiate a user session by calling LMChatCore.showChat().

val apiKey = "Your generated API key" // api key generated from the dashboard
val userName = "ENTER USER NAME" // name of the user
val userId = "ENTER USER ID" // id of the user
val context = this // instance of context

val successCallback = { response : UserResponse? ->
//user session initiated successfully, write your logic here
Unit
} // callback triggered when the initiate user call is successful

val failureCallback = { errorMessage ->
Log.e("Example", errorMessage)
Unit
} // callback triggered when the initiate user call fails

LMChatCore.showChat(
context = context,
apiKey = apiKey,
uuid = userId,
userName = userName,
success = successCallback,
error = failureCallback
)

2. Server Side Authentication (Without API Key)

This approach should be used when you want to manage LikeMinds Authentication tokens on your backend server. In this case you eliminate the need to expose your API Key in your client app and your backend server is responsible for calling the Initiate API to obtain the accessToken and refreshToken which is passed to LMChatCore.showChat() to validate the user session.

  1. Create a function to get accessToken and refreshToken from your backend using Initiate API
suspend fun getTokens():Pair<String,String>{
// your implementation to fetch LikeMinds authentication tokens

return Pair(accessToken, refreshToken)
}
  1. While setting up LikeMinds Chat SDK in onCreate() method of the Application class, extend LMChatCoreCallback and pass the instance of the same in LMChatCore.setup()
val application = this // instance of the application
val lmChatTheme = LMChatTheme.COMMUNITY_CHAT // chat theme choosen
val lmChatAppearanceRequest = null // instance of the appearance
val domain = "ENTER YOUR DOMAIN NAME" // domain of the app
val enablePushNotifications = false // enable or disable push notifications
val deviceId = null // device id of the user

val lmChatCoreCallback = object : LMChatCoreCallback {
override fun onAccessTokenExpiredAndRefreshed(
accessToken: String,
refreshToken: String
) {
Log.d("Example","accessToken: $accessToken, refreshToken: $refreshToken")
}

override fun onRefreshTokenExpired(): Pair<String?, String?> {
return runBlocking {
getTokens()
}
}
}

LMChatCore.setup(
application = application,
theme = lmChatTheme,
lmChatCoreCallback = lmChatCoreCallback,
lmChatAppearanceRequest = lmChatAppearanceRequest,
domain = domain,
enablePushNotifications = enablePushNotifications,
deviceId = deviceId
)
info

LMChatCoreCallback has two callbacks:

  1. onAccessTokenExpiredAndRefreshed(): This callback is triggered when the provided accessToken expires and is refreshed internally using the refreshToken.
  2. onRefreshTokenExpired() This callback is triggered when the provided refreshToken expires. In this case, you need to provide a new accessToken and refreshToken from your backend server using our initiate API.
  1. Upon receiving the accessToken and refreshToken from your backend server, call LMChatCore.showChat() function with these tokens.
val context = this // instance of context
val accessToken = "RECEIVED ACCESS TOKEN"
val refreshToken = "RECEIVED REFRESH TOKEN"

val successCallback = { response : UserResponse? ->
//user session initiated successfully, write your logic here
Unit
} // callback triggered when the initiate user call is successful

val failureCallback = { errorMessage ->
Log.e("Example", errorMessage)
Unit
} // callback triggered when the initiate user call fails

LMChatCore.showChat(
context = context,
accessToken = accessToken,
refreshToken = refreshToken,
success = successCallback,
error = failureCallback
)
tip

By choosing the appropriate method based on your backend infrastructure and security preferences, you can seamlessly integrate the Chat SDK into your application while ensuring secure and efficient session management.

Step 4 - Navigation to the chat

Once you have initiated the user session, you can navigate the user to your Chat in the above mentioned successCallback to be passed in LMChatCore.showChat(). LikeMinds provide various Chat Themes to which you can navigate with the help of following code:

// pass this successCallback to LMChatCore.showChat()
val successCallback = { response : UserResponse? ->
// inflate community chat fragment in your activity
val containerViewId = R.id.frame_layout
val fragment = CommunityChatFragment.getInstance()

val transaction = supportFragmentManager.beginTransaction()
transaction.replace(containerViewId, fragment, containerViewId.toString())
transaction.commit()
Unit
} // callback triggered when the initiate user call is successful

That's it! You have successfully integrated the LikeMinds Chat SDK into your Android application. The next step would be to explore additional customization options or configurations provided by the SDK to tailor the chat to your application's needs.