In most cases, you will already have an existing application that you want to add chat functionality to.
In this tutorial, we will build a simple delivery app first to demonstrate how to integrate UIKit into an existing app. This app requires a chat feature so that users can communicate with their delivery person.
To get started, clone the starter code from the GitHub repository.
You can find the code in the integrate-with-existing-app/before folder.
The app basically has two screens as follows:
Login Screen: A simple form to enter the user ID.
Order Status Screen: A screen to show the order details and the status.
First, initialize SendbirdUIKit in the Application class.
in getAppId(), specify your Sendbird application ID.
You can specify user using getUserInfo() function. The user id will be determined by the user's input in the login screen.
getUserInfo() is not called immediately after the app starts. It is called when it's required to connect to Sendbird server.
package com.sendbird.uikit.tutorial
import android.app.Application
import com.sendbird.android.exception.SendbirdException
import com.sendbird.android.handler.InitResultHandler
import com.sendbird.uikit.SendbirdUIKit
import com.sendbird.uikit.adapter.SendbirdUIKitAdapter
import com.sendbird.uikit.interfaces.UserInfo
class BaseApplication : Application() {
override fun onCreate() {
super.onCreate()
SendbirdUIKit.init(object : SendbirdUIKitAdapter {
override fun getAppId(): String {
return TODO("Specify your Sendbird application ID.") // YOUR_APP_ID
}
override fun getAccessToken(): String {
return ""
}
override fun getUserInfo(): UserInfo {
return object : UserInfo {
override fun getUserId(): String {
return getSharedPreferences("sendbird", MODE_PRIVATE).getString("user_id", "")!!
// Use the ID of a user you've created on the dashboard.
// If there isn't one, specify a unique ID so that a new user can be created with the value.
}
override fun getNickname(): String {
return "" // Specify your user nickname. Optional.
}
override fun getProfileUrl(): String {
return ""
}
}
}
override fun getInitResultHandler(): InitResultHandler {
return object : InitResultHandler {
override fun onMigrationStarted() {
// DB migration has started.
}
override fun onInitFailed(e: SendbirdException) {
// If DB migration fails, this method is called.
}
override fun onInitSucceed() {
// If DB migration is successful, this method is called and you can proceed to the next step.
// In the sample app, the `LiveData` class notifies you on the initialization progress
// And observes the `MutableLiveData<InitState> initState` value in `SplashActivity()`.
// If successful, the `LoginActivity` screen
// Or the `HomeActivity` screen will show.
}
}
}
}, this)
}
}
And register the BaseApplication class in the AndroidManifest.xml file. Put this code inside the <application> tag.