You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Sendbird Live and Sendbird Chat together, how do I handle initialization and connection on foreground (Activity or Fragment)?
Guide & Snippet
After introducing Android Jetpack library, customers want to setup the initialization on Activity or Fragment, but it is not recommended to initialize the SDK in the Activity or Fragment because the SDK should be initialized only once in the application lifecycle.
To handle the initialization and connection on the foreground, you can set the InitParams.isForeground parameter to true when initializing the SDK.
The SendbirdLive.init calls the SendbirdChat.init function internally, so you don't need to call the SendbirdChat.init function separately.
The SendbirdLive.authenticate calls the SendbirdChat.connect function internally, so you don't need to call the SendbirdChat.connect function separately.
The logic below is a custom snippet with a simple example.
packagecom.sendbird.uikitwithlive.live_chat_for_blacklabelimportandroid.os.Bundleimportandroidx.activity.ComponentActivityimportandroidx.activity.compose.setContentimportcom.sendbird.android.SendbirdChatimportcom.sendbird.android.channel.BaseChannelimportcom.sendbird.android.channel.GroupChannelimportcom.sendbird.android.handler.GroupChannelHandlerimportcom.sendbird.android.message.BaseMessageimportcom.sendbird.android.params.GroupChannelCreateParamsimportcom.sendbird.live.AuthenticateParamsimportcom.sendbird.live.handler.InitResultHandlerimportcom.sendbird.live.InitParamsimportcom.sendbird.live.SendbirdLiveimportcom.sendbird.webrtc.SendbirdExceptionvalAPP_ID="YOUR_APP_ID"valUSER_ID="YOUR_USER_ID"classMainActivity : ComponentActivity() {
overridefunonCreate(savedInstanceState:Bundle?) {
super.onCreate(savedInstanceState)
initSendbirdLive()
setContent {
// Composable
}
}
privatefuninitSendbirdLive() {
val params =InitParams(APP_ID, this).apply {
// let SDK know it's called from foreground
isForeground =true
}
SendbirdLive.init(params, object:InitResultHandler {
overridefunonInitFailed(e:SendbirdException) {
// Handle error.
}
overridefunonInitSucceed() {
authenticateSendbirdLive()
}
overridefunonMigrationStarted() {
}
})
}
privatefunauthenticateSendbirdLive() {
val params =AuthenticateParams(USER_ID, null)
SendbirdLive.authenticate(params) { user, e ->if (e !=null) {
//handle errorreturn@authenticate
}
createChannel()
// The user has been authenticated successfully and is connected to Sendbird server.
}
SendbirdChat.addChannelHandler("channelHandler", object:GroupChannelHandler() {
overridefunonMessageReceived(channel:BaseChannel, message:BaseMessage) {
// Handle received message.
}
overridefunonMessageUpdated(channel:BaseChannel, message:BaseMessage) {
// Handle updated message.
}
})
}
privatefuncreateChannel() {
// Create a channelGroupChannel.createChannel(
GroupChannelCreateParams().apply {
userIds =listOf(USER_ID)
}
) { channel, e ->if (e !=null) {
// Handle error.
} else {
// Handle success.
}
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Summary
Guide & Snippet
InitParams.isForeground
parameter totrue
when initializing the SDK.SendbirdLive.init
calls theSendbirdChat.init
function internally, so you don't need to call theSendbirdChat.init
function separately.SendbirdLive.authenticate
calls theSendbirdChat.connect
function internally, so you don't need to call theSendbirdChat.connect
function separately.Reference
Beta Was this translation helpful? Give feedback.
All reactions