This tutorial teaches you to integrate Sendbird's Chat UIKit for Jetpack Compose in your Android application.
This is what your app will look like when you've finished this tutorial:
Left: channel list view. Right: channel view.
Before you start, you need the following:
Next, create a user in your Sendbird application:
Kotlin
and the minimum SDK as API 24: Android 7.0 (Nougat)
and click Finish.Install UIKit for Jetpack Compose using Gradle by following the steps below.
Add the following to your settings.gradle.kts
(Project Settings) file:
dependencyResolutionManagement {
repositories {
maven { setUrl("https://repo.sendbird.com/public/maven") }
}
}
Note: You should be using Gradle 6.8 or higher. You can check the gradle-wrapper.properties
file in your project to see the version of Gradle you are using.
Add the dependency to your build.gradle.kts
(Module) file:
dependencies {
implementation("com.sendbird.sdk:uikit-compose:1.0.0")
}
Then, click the Sync button to apply all changes.
To properly integrate and initialize Sendbird UIKit for Jetpack Compose in your Android project, follow these steps to create and set up the BaseApplication.kt
file.
BaseApplication
.package com.example.uikittutorial
import android.app.Application
import com.sendbird.uikit.compose.SendbirdUikitCompose
import com.sendbird.uikit.core.data.model.UikitCurrentUserInfo
import com.sendbird.uikit.core.data.model.UikitInitParams
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.launchIn
const val AppId = "YOUR_APP_ID"
const val UserId = "YOUR_USER_ID"
class BaseApplication : Application() {
override fun onCreate() {
super.onCreate()
// Initialize Sendbird UIKit Compose.
SendbirdUikitCompose.init(
UikitInitParams(
appId = AppId,
context = this,
isForeground = true
)
).launchIn(MainScope())
// Prepare user information.
SendbirdUikitCompose.prepare(
UikitCurrentUserInfo(
userId = UserId,
authToken = "USER_ACCESS_TOKEN"
)
)
}
}
Note: You can find the user's Access token in the Sendbird Dashboard under your Application > Users > your user > Access token. For this tutorial, you are using the user access token as a way of authentication. For actual implementation, it is highly recommended to refer to this authentication guide to implement a more secure way of authentication.
To ensure that your BaseApplication
class is used as the application class, you need to register it in the AndroidManifest.xml
file.
<application
android:name=".BaseApplication"
>
</application>
To start with the Channel List as the starting screen, declare it as follows in your MainActivity
:
Note: If your app doesn't require a Channel List View, you can skip this step and go on to step 7: Display Channel view.
package com.example.uikittutorial
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.example.uikittutorial.ui.theme.UIKitTutorialTheme
import com.sendbird.uikit.compose.navigation.SendbirdNavigation
import com.sendbird.uikit.compose.navigation.sendbirdGroupChannelNavGraph
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
UIKitTutorialTheme {
SendbirdUikitApp()
}
}
}
}
@Composable
fun SendbirdUikitApp() {
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = SendbirdNavigation.GroupChannel.route
) {
sendbirdGroupChannelNavGraph(navController = navController)
}
}
Press the Run app button to launch the app on the emulator in AndroidStudio. The list of channels associated with the indicated user will be displayed as the starting screen, as shown below.
To directly display the channel view, set the startDestination
to SendbirdNavigationRoute.Channel
. This bypasses the channel list view and takes you straight to the channel view. You must pass the channelUrl
as a required parameter, which can be obtained on the Sendbird Dashboard under your Application > Moderation > Channel Moderation > your channel.
const val CHANNEL_URL = "YOUR CHANNEL_URL"
@Composable
fun SendbirdUikitApp() {
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = SendbirdNavigation.GroupChannel.route
) {
sendbirdGroupChannelNavGraph(
navController = navController,
startDestination = SendbirdNavigationRoute.Channel(channelUrl = CHANNEL_URL)
)
}
}
Press the Run app button to launch the app on the emulator in AndroidStudio. The app will start from the ChannelScreen
as shown below.
You've successfully integrated Sendbird's UIKit for Jetpack Compose in your Android application. You've learned how to:
You can customize the UIKit to match your app's design.