Getting Started
The LikeMinds Android Feed SDK empowers you to integrate personalized and engaging feeds into your Android application, enhancing user experiences and driving user engagement. This guide will walk you through the steps to get started with the LikeMinds Android Feed SDK and set up a dynamic feed in your application. Obtain the necessary API key from the LikeMinds dashboard.
Prerequisites
Before you begin, ensure that you have the following:
- Kotlin Version: Your project's kotlin version should be greater than 1.6.0.
- Minimun Android SDK Version:
minSdk
should be atleast 21. - Dependency Resolution Management:
mavenCentral()
andjitpack.io
should be mentioned in dependencyResolutionManagement.
Step-by-Step Integration Guide
Follow these steps to integrate the LikeMinds Feed SDK into your Android application using MavenCentral:
Step 1 - Installation
Implement likeminds-feed-core
project in your app level build.gradle
.
dependencies {
...
implementation 'community.likeminds:likeminds-feed-core:1.5.1'
}
Set dataBinding true
in your app level build.gradle
if not done already.
Now, sync the gradle before moving to next step.
Step 2 - Setup LikeMinds Feed
Initiate LMFeedCore
in onCreate()
method of the Application class using the following code:
- Kotlin
- Java
val application = this
val theme = LMFeedTheme.SOCIAL_FEED
val enablePushNotifications = false
val deviceId = null
val domain = "ENTER YOUR DOMAIN HERE"
val lmFeedCoreCallback = null
LMFeedCore.setup(
application = application,
theme,
enablePushNotifications = enablePushNotifications,
deviceId = deviceId,
domain = domain,
lmFeedCoreCallback = lmFeedCoreCallback
)
Application applicationInstance = this; // instance of your application
LMFeedTheme theme = LMFeedTheme.SOCIAL_FEED; // theme selected for your app
String domain = "ENTER YOUR DOMAIN HERE"; // your domain
boolean enablePushNotifications = false; // whether to enable push notifications or not
String deviceId = null; // id of the device
LMFeedAppearanceRequest appearanceRequest = new LMFeedAppearanceRequest.Builder().build(); // object of [LMFeedAppearanceRequest]
LMFeedCoreCallback coreCallback = null; // object of feed core callback
LMFeedCore.INSTANCE.setup(
applicationInstance, // instance of the application
theme,
domain, // your domain
enablePushNotifications, // enable push notifications
deviceId, // device id
appearanceRequest, // object of [LMFeedAppearanceRequest] to set appearance of Feed
coreCallback // callback for various operations in SDK
);
VARIABLE | TYPE | DESCRIPTION | OPTIONAL |
---|---|---|---|
application | Application | Instance of your application class. | |
theme | LMFeedTheme | Theme selected for theme. | |
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 | ✔ |
lmFeedAppearance | LMFeedAppearanceRequest | Request object to set appearance of the Feed. | ✔ |
lmFeedCoreCallback | LMFeedCoreCallback | Callback for various operations in SDK . | ✔ |
Step 3 - Initiate User Session
You have successfully initiated the LMFeedCore
. Now, you have to initiate a user session. You can initiate the user session using one of the following two approaches:
1. 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 Feed SDK, which will be used to initiate a user session by calling LMFeedCore.showFeed()
.
- 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? ->
//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
LMFeedCore.showFeed(
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;
LMFeedCore.INSTANCE.showFeed(
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;
}
);
2. 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 LMFeedCore.showFeed()
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 Feed SDK in
onCreate()
method of the Application class, extendLMFeedCoreCallback
and pass the instance of the same inLMFeedCore.setup()
- Kotlin
- Java
val application = this
val theme = LMFeedTheme.SOCIAL_FEED
val enablePushNotifications = false
val deviceId = null
val domain = "ENTER YOUR DOMAIN HERE"
val lmFeedCoreCallback = object : LMFeedCoreCallback {
override fun onAccessTokenExpiredAndRefreshed(
accessToken: String,
refreshToken: String
) {
Log.d("Example","accessToken: $accessToken, refreshToken: $refreshToken")
}
override fun onRefreshTokenExpired(): Pair<String?, String?> {
return runBlocking{
getTokens()
}
}
}
LMFeedCore.setup(
application = application,
theme,
enablePushNotifications = enablePushNotifications,
deviceId = deviceId,
domain = domain,
lmFeedCoreCallback = lmFeedCoreCallback
)
Application applicationInstance = this;
LMFeedTheme theme = LMFeedTheme.SOCIAL_FEED;
String domain = "ENTER YOUR DOMAIN HERE";
boolean enablePushNotifications = true;
String deviceId = "ENTER YOUR DEVICE ID HERE";
LMFeedAppearanceRequest appearanceRequest = new LMFeedAppearanceRequest.Builder().build();
LMFeedCoreCallback coreCallback = new LMFeedCoreCallback() {
@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();
}
};
LMFeedCore.INSTANCE.setup(
applicationInstance, // instance of your application
theme, // theme selected for your app
domain, // your domain
enablePushNotifications, // enable push notifications
deviceId, // device id
appearanceRequest, // object of [LMFeedAppearanceRequest] to set appearance of Feed
coreCallback // callback for various operations in SDK
);
LMFeedCoreCallback
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, callLMFeedCore.showFeed()
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? ->
//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
LMFeedCore.showFeed(
context = context,
accessToken = accessToken,
refreshToken = refreshToken,
success = successCallback,
error = failureCallback
)
Context context = this;
String accessToken = "RECEIVED ACCESS TOKEN";
String refreshToken = "RECEIVED REFRESH TOKEN";
LMFeedCore.INSTANCE.showFeed(
context,
accessToken,
refreshToken,
(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;
}
);
Step 4 - Navigation to the feed
Once you have initiated the user session, you can navigate the user to your Feed in the above mentioned successCallback
to be passed in LMFeedCore.showFeed()
. LikeMinds provide various Feed Themes to which you can navigate with the help of following code:
- Social Feed
- Video Feed
- QnA Feed
- Kotlin
- Java
// pass this successCallback to LMFeedCore.showFeed()
val successCallback = { response : UserResponse? ->
// inflate social feed fragment in your activity
val containerViewId = R.id.frame_layout
val fragment = LMFeedSocialFeedFragment()
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 LMFeedCore.showFeed()
(UserResponse response) -> {
// callback triggered when the initiate user call is successful
try {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new LMFeedSocialFeedFragment()).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 LMFeedCore.showFeed()
val successCallback = { response : UserResponse? ->
// inflate video feed fragment in your activity
val containerViewId = R.id.frame_layout
val fragment = LMFeedVideoFeedFragment()
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 LMFeedCore.showFeed()
(UserResponse response) -> {
// callback triggered when the initiate user call is successful
try {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new LMFeedVideoFeedFragment()).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 LMFeedCore.showFeed()
val successCallback = { response : UserResponse? ->
// inflate qna feed fragment in your activity
val containerViewId = R.id.frame_layout
val fragment = LMFeedQnAFeedFragment()
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 LMFeedCore.showFeed()
(UserResponse response) -> {
// callback triggered when the initiate user call is successful
try {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new LMFeedQnAFeedFragment()).commit();
} catch (Exception e) {
throw new RuntimeException(e);
}
// callback triggered when the initiate user call is successful, write your logic here
return null;
}
By choosing the appropriate method based on your backend infrastructure and security preferences, you can seamlessly integrate the Feed SDK into your application while ensuring secure and efficient session management.
That's it! You have successfully integrated the LikeMinds Feed SDK into your Android application. The next step would be to explore additional customization options or configurations provided by the SDK to tailor the feed to your application's needs.