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
- Kotlin Version: Your project's kotlin version should be greater than 1.6.0.
- Minimum Android SDK Version:
minSdk
should be at least 21. - Dependency Resolution Management:
mavenCentral()
andjitpack.io
should be mentioned independencyResolutionManagement
. - 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
- 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
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
- 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 navigation safe args gradle plugin version, go to lm-chat-root-dependencies.gradle
and change lm_chat_versions.navigation
to your version.
- 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
Now that you have installed the SDK, let's set up the LikeMinds Chat in your application. You'll need to initialize LMChatCore
in your Application class.
Here are the parameters you'll need to configure:
VARIABLE | TYPE | DESCRIPTION | OPTIONAL |
---|---|---|---|
application | Application | Instance of your application class. | |
theme | LMChatTheme | Theme selected for the chat, possible values - COMMUNITY_CHAT - NETWORKING_CHAT - COMMUNITY_HYBRID_CHAT - AI_CHATBOT | |
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 | ✔ |
lmChatAppearanceRequest | LMChatAppearanceRequest | Request object to set appearance to the Chat. | ✔ |
lmChatCoreCallback | LMChatCoreCallback | Callback for various operations in SDK. | ✔ |
shareLogsWithLM | Boolean | Whether to share error logs with LikeMinds or not | ✔ |
excludeConversationStates | List<ConversationState> | List of conversation states to be excluded in Chatroom | ✔ |
Add the following code in onCreate()
method of your Application class:
- Kotlin
- Java
val application = this // instance of the application
val lmChatTheme = LMChatTheme.COMMUNITY_CHAT // chat theme chosen
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
val shareLogsWithLM = true // whether to share error logs with LM
val excludeConversationStates = emptyList()
LMChatCore.setup(
application = application,
theme = lmChatTheme,
lmChatCoreCallback = lmChatCoreCallback,
lmChatAppearanceRequest = lmChatAppearanceRequest,
domain = domain,
enablePushNotifications = enablePushNotifications,
deviceId = deviceId,
shareLogsWithLM = shareLogsWithLM,
excludeConversationStates = excludeConversationStates
)
Application application = this; // instance of the application
LMChatCoreCallback lmChatCoreCallback = null; // instance of the callback
LMChatTheme lmChatTheme = LMChatTheme.COMMUNITY_CHAT; // instance of the theme
LMChatAppearanceRequest lmChatAppearanceRequest = null;
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
boolean shareLogsWithLM = true;
List<ConversationState> excludeConversationStates = new ArrayList<>();
LMChatCore.INSTANCE.setup(
application,
lmChatTheme,
lmChatCoreCallback,
lmChatAppearanceRequest,
domain,
enablePushNotifications,
deviceId,
shareLogsWithLM,
excludeConversationStates
);
Step 3 - Initiate User Session
After setting up LMChatCore
, you need to initiate a user session. This step is crucial for identifying users in your chat system.
- Kotlin
- Java
val apiKey = "ENTER YOUR 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
)
String apiKey = "ENTER YOUR 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, write your logic here
return null;
},
(String error) -> {
// callback triggered when the initiate user call fails
Log.e("Example", error);
return null;
}
);
For enhanced security, you can use Server Side User Authentication to initiate user sessions through your own server.
Step 4 - Navigate to Chat
Once you have initiated the user session, you can navigate the user to your Chat in the successCallback
passed to LMChatCore.showChat()
. LikeMinds provides various Chat Themes that you can implement using the following code:
This step is not required in AI Chatbot Theme. Navigate to Step 5 to continue.
- Community Chat
- Networking Chat
- Community Hybrid Chat
- Kotlin
- Java
// 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
// pass this callback to LMChatCore.showChat()
(UserResponse response) -> {
// inflate community chat fragment in your activity
try {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, CommunityChatFragment.getInstance()).commit();
} catch (Exception e) {
throw new RuntimeException(e);
}
// callback triggered when the initiate user call is successful, write your logic here
return null;
}
- Kotlin
- Java
// pass this successCallback to LMChatCore.showChat()
val successCallback = { response : UserResponse? ->
// inflate networking chat fragment in your activity
val containerViewId = R.id.frame_layout
val fragment = NetworkingChatFragment.getInstance()
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(containerViewId, fragment, containerViewId.toString())
transaction.commit()
Unit
} // callback triggered when the initiate user call is successful
// pass this callback to LMChatCore.showChat()
(UserResponse response) -> {
// inflate community chat fragment in your activity
try {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, NetworkingChatFragment.Companion.getInstance(null)).commit();
} catch (Exception e) {
throw new RuntimeException(e);
}
// callback triggered when the initiate user call is successful, write your logic here
return null;
}
By default, only Members to Community Managers and vice versa direct messages are allowed. To enable direct messages for all Members, enable Members can DM other members from LikeMinds Dashboard's Chat Setting Section.
- Kotlin
- Java
// pass this successCallback to LMChatCore.showChat()
val successCallback = { response : UserResponse? ->
// inflate community hybrid chat fragment in your activity
val containerViewId = R.id.frame_layout
val fragment = CommunityHybridChatFragment.getInstance()
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(containerViewId, fragment, containerViewId.toString())
transaction.commit()
Unit
} // callback triggered when the initiate user call is successful
// pass this callback to LMChatCore.showChat()
(UserResponse response) -> {
// inflate community hybrid chat fragment in your activity
try {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, CommunityHybridChatFragment.Companion.getInstance()).commit();
} catch (Exception e) {
throw new RuntimeException(e);
}
// callback triggered when the initiate user call is successful, write your logic here
return null;
}
By default, only Members to Community Managers and vice versa direct messages are allowed. To enable direct messages for all Members, enable Members can DM other members from LikeMinds Dashboard's Chat Setting Section.
Step 5 - Add Chatbot Button to your Screen (Optional)
Add AI Assitant to LikeMinds: Follow this tutorial to add Assistant to LikeMinds Dashboard, before proceeding with this step.
Add AI Chatbot Button to your Screen
If you're using the AI Chatbot theme, you can add a FAB using LMChatAIBotButton
in your screen's XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.likeminds.chatmm.aichatbot.LMChatAIBotButton
android:id="@+id/btn_ai_chatbot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Start User Session and Start AI Chatbot
Then, initiate the user session in your Activity or Fragment:
- Kotlin
- Java
val apiKey = "ENTER YOUR 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
findViewById<LMChatAIBotButton>(R.id.btn_ai_chatbot).setChatAIButtonProps(
LMChatAIBotButtonProps.Builder()
.uuid(userId)
.apiKey(apiKey)
.userName(userName)
.build()
)
String apiKey = "ENTER YOUR 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
((LMChatAIBotButton) findViewById(R.id.btn_ai_chatbot)).setChatAIButtonProps(
new LMChatAIBotButtonProps.Builder()
.uuid(userId)
.apiKey(apiKey)
.userName(userName)
.build()
);
For enhanced security, you can use Server Side User Authentication to initiate user sessions through your own server.
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.