How to build a chronological or custom activity feed in your Android app

Italo Orihuela
Amity Developers
Published in
3 min readAug 27, 2023

--

For social media and community platforms, the global feed is a crucial component. It’s the hub where users can discover and engage with the latest content. Today, we’ll be exploring two distinct methods of implementing a global feed using the Amity Social Cloud SDK: the `getGlobalFeed` and `getCustomPostRanking` methods. These methods allow you to customize your content experience to better align with your users’ interests and preferences.

Whether you’re looking to display posts in chronological order or use a score-sorting mechanism to highlight the most engaging content, this tutorial will guide you through the process. So, let’s dive in and explore how to create a more dynamic and engaging community experience for your users.

Pre-requisites:

  • Amity SDK installed and set up in your Android project.
  • An instance of `AmityFeedRepository` available.

Installing the Amity Android SDK

Before we start, it’s important to ensure that the Amity Android SDK is installed and set up in your Android project. Here are the requirements and steps to install the SDK:

Requirements

  • Android 5.0 (API level 21) and above
  • Target sdk version 29 and above
  • Compile SDK Version 29 and above
  • JVM target should be 1.8

Installation

Add the Jitpack repository in your project level `build.gradle` at the end of repositories:

If you’re using Gradle 6.8 or above:


dependencyResolutionManagement {
repositories {

maven { url 'https://jitpack.io' }
}
}

If you’re using Gradle 6.7 or below:

allprojects {
repositories {

maven { url 'https://jitpack.io' }
}
}

Add the dependency in your module level `build.gradle`. Find the latest SDK version at Changelog.

If you’re using Gradle groovy:

implementation 'com.github.AmityCo.Amity-Social-Cloud-SDK-Android:amity-sdk:x.y.z'

If you’re using Kotlin Gradle DSL:

implementation("com.github.AmityCo.Amity-Social-Cloud-SDK-Android:amity-sdk:x.y.z")

Initializing the SDK

After installing the SDK, you need to initialize it in your application class. Here’s how you can do it:

class ChatApp : Application() {
//
override fun onCreate() {
super.onCreate()
//
AmityCoreClient.setup(
apiKey = "apikey",
endpoint = AmityEndpoint.EU // optional param, defaulted as SG region
)
}
}

In the above code, `ChatApp` is your application class. The `AmityCoreClient.setup( )` method initializes the Amity SDK with the application context.

The following code, authenticates the session:

fun authenticate() {
AmityCoreClient.login(userId = "userId 1")
.displayName(displayName = "John Doe") // optional
.authToken(authToken = "token") // optional
.build()
.submit()
.doOnComplete {
//success
}
.subscribe()
}

Step 1: Import the necessary classes

Make sure to import the necessary classes. This includes the `AmityFeedRepository`, `AmityPost`, and `AmityPagingData` classes.

import com.amity.socialcloud.sdk.social.feed.AmityFeedRepository;
import com.amity.socialcloud.sdk.social.model.AmityPost;
import com.amity.socialcloud.sdk.core.pagination.AmityPagingData;

Step 2: Query the Global Feed in Chronological Order

The `getGlobalFeed` method allows you to retrieve posts in chronological order. Here’s how you can implement it:

fun queryGlobalFeed(feedRepository: AmityFeedRepository) {
feedRepository
.getGlobalFeed()
.build()
.getPagingData()
.doOnNext { pagingData: PagingData<AmityPost> ->
//results
}
.subscribe()
}

Step 3: Query the Global Feed with Custom Post Ranking

If you want to present posts based on their relevance, you can use the `getCustomRankingGlobalFeed` method. This method uses a score-sorting mechanism to ensure the most engaging content is at the top of the feed.

fun queryCustomRankingFeed(feedRepository: AmityFeedRepository) {
feedRepository
.getCustomRankingGlobalFeed()
.build()
.getPagingData()
.doOnNext { pagingData: PagingData<AmityPost> ->
//results
}
.subscribe()
}

In both methods, replace `//results` with your own code to handle the results. The `subscribe()` method is used to start the query. The `doOnNext` method is called each time a new page of results is loaded. The `PagingData<AmityPost>` object contains the posts in the current page.

Final Thoughts

Implementing a global feed that aligns with your users’ interests and preferences can significantly enhance the user experience on your platform. With the `getGlobalFeed` and `getCustomPostRanking` methods provided by the Amity SDK, you can easily customize your global feed to display posts in chronological order or based on their relevance. We hope this tutorial has been helpful in guiding you through the process.

--

--