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:
Step-by-Step Integration
Step 1 - Installation
- Generate API key from the LikeMinds Dashboard.
a. Enable Chatrooms and Direct Messages settings from Features & Settings section on the LikeMinds Dashboard. - Fork our repository Chat Repository from Github.
- 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
- Include the
chatmm
project in your project by pasting the following code in yoursettings.gradle
.
include ':chatmm'
project(':chatmm').projectDir = new File(rootDir, '<name-of-the-forked-project>/chatmm/')
- 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'
}
}
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.
- Add the following code (if not already added) in your dependency resolution management.
maven { url "https://jitpack.io" }
- Enable
dataBinding
in your project (if not done already) by adding the following code in your app levelbuild.gradle
.
android {
buildFeatures {
dataBinding = true
}
}
- 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:
- Kotlin
- Java
val application = this // instance of the application
val lmChatCoreCallback= null // instance of the callback
val chatTheme = null // instance of the theme
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,
lmChatCoreCallback = lmChatCoreCallback,
theme = chatTheme,
domain = domain,
enablePushNotifications = enablePushNotifications,
deviceId = deviceId
)
Application application = this; // instance of the application
LMChatCoreCallback lmChatCoreCallback = null; // instance of the callback
LMChatTheme chatTheme = null; // instance of the theme
String domain = "ENTER YOUR DOMAIN NAME"; // domain of the app
boolean enablePushNotifications = false; // enable or disable push notifications
String deviceId = null; // device id of the user
LMChatCore.INSTANCE.setup(
application,
lmChatCoreCallback,
chatTheme,
domain,
enablePushNotifications,
deviceId
);
VARIABLE | TYPE | DESCRIPTION | OPTIONAL |
---|---|---|---|
application | Application | Instance of your application class. | |
domain | String | Your domain url. | ✔ |
enablePushNotifications | Boolean | Whether to enable push notifications or not | ✔ |
deviceId | String | Unique Id of the device, if notifications are enabled | ✔ |
lmChatTheme | LMChatTheme | Request object to set theme to the Chat. | ✔ |
lmChatCoreCallback | LMChatCoreCallback | Callback for various operations in SDK . | ✔ |
Step 3 - Inflate GroupChat and Direct Messaging
You have successfully initiated the LMChatCore
, now all you need to do is inflate Chat Fragment in onCreate()
method in your activity. Now, You have the option to initiate a user session and navigate to Chat Fragment using one of two approaches:
1. With API Key (Client Side Authentication)
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()
.
- Kotlin
- Java
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? ->
// inflate chat fragment in your activity
val containerViewId = R.id.frame_layout
val fragment = LMChatFragment()
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(containerViewId, fragment, containerViewId.toString())
transaction.commit()
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
)
String apiKey = "Your generated API key"; // api key generated from the dashboard
String userId = "ENTER USER ID"; // id of the user
String userName = "ENTER USER NAME"; // name of the user
Context context = this;
LMChatCore.INSTANCE.showChat(
context,
apiKey,
userId,
userName,
(UserResponse response) -> {
// callback triggered when the initiate user call is successful
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new LMChatFragment()).commit();
return null;
},
(String error) -> {
// callback triggered when the initiate user call fails
Log.e("Example", error);
return null;
}
);
2. Without API Key (Server Side Authentication)
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.
- Create a function to get
accessToken
andrefreshToken
from your backend using Initiate API
- Kotlin
- Java
suspend fun getTokens():Pair<String,String>{
// your implementation to fetch LikeMinds authentication tokens
return Pair(accessToken, refreshToken)
}
void getTokens(){
Thread thread = new Thread(() -> {
// your implementation to fetch LikeMinds authentication tokens
});
thread.start();
}
- While setting up LikeMinds Chat SDK in
onCreate()
method of the Application class, extendLMChatCoreCallback
and pass the instance of the same inLMChatCore.setup()
- Kotlin
- Java
val application = this
val enablePushNotifications = false
val deviceId = null
val domain = "ENTER YOUR DOMAIN NAME HERE"
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,
enablePushNotifications = enablePushNotifications,
deviceId = deviceId,
domain = domain,
lmChatCoreCallback = lmChatCoreCallback
)
Application application = this; // instance of the application
LMChatTheme chatTheme = null; // instance of the theme
String domain = "ENTER YOUR DOMAIN NAME"; // domain of the app
boolean enablePushNotifications = false; // enable or disable push notifications
String deviceId = null; // device id of the user
LMChatCoreCallback lmChatCoreCallback = new LMChatCoreCallback() {
@Override
public void onAccessTokenExpiredAndRefreshed(@NonNull String accessToken, @NonNull String refreshToken) {
Log.d("Example","accessToken:" + accessToken + "refreshToken:" + refreshToken);
}
@NonNull
@Override
public Pair<String, String> onRefreshTokenExpired() {
return getTokens();
}
};
LMChatCore.INSTANCE.setup(
application,
lmChatCoreCallback,
chatTheme,
domain,
enablePushNotifications,
deviceId
);
LMChatCoreCallback
has two callbacks:
onAccessTokenExpiredAndRefreshed()
: This callback is triggered when the providedaccessToken
expires and is refreshed internally using therefreshToken
.onRefreshTokenExpired()
This callback is triggered when the providedrefreshToken
expires. In this case, you need to provide a newaccessToken
andrefreshToken
from your backend server using our initiate API.
- Upon receiving the
accessToken
andrefreshToken
from your backend server, callLMChatCore.showChat()
function with these tokens.
- Kotlin
- Java
val context = this // instance of context
val accessToken = "RECEIVED ACCESS TOKEN"
val refreshToken = "RECEIVED REFRESH TOKEN"
val successCallback = { response : UserResponse? ->
// inflate chat fragment in your activity
val containerViewId = R.id.frame_layout
val fragment = LMChatFragment()
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(containerViewId, fragment, containerViewId.toString())
transaction.commit()
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
)
Context context = this;
String accessToken = "RECEIVED ACCESS TOKEN";
String refreshToken = "RECEIVED REFRESH TOKEN";
LMChatCore.INSTANCE.showChat(
context,
accessToken,
refreshToken,
(UserResponse response) -> {
// callback triggered when the initiate user call is successful
try {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new LMChatFragment()).commit();
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
},
(String error) -> {
// callback triggered when the initiate user call fails
Log.e("Example", error);
return null;
}
);
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.
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.