You can integrate Sendbird Calls to Sendbird Chat to provide users with a seamless experience in using Calls and Chat services by allowing them to make a call within a chat channel.
With the implementation of Calls integration to Chat, the call screen will appear when the call starts and when the call ends users will return to the chat view.
Note: To turn on Calls integration to Chat on the Sendbird Dashboard, go to Settings > Chat > Messages.
Since Calls integration to Chat is a way to add call capabilities to Sendbird Chat, it requires an app that uses Chat. If you already have one, you are ready to move to the next step. If you don’t have an app, learn how to set up Sendbird Chat from our Send first message page.
Sendbird Chat SDK and Sendbird Calls SDK both use singleton interfaces. Since these two SDKs work independently, create appropriate functions that can handle them together.
To log in to the Calls and Chat SDKs, create a function that allows you to authenticate the Calls SDK and the Chat SDK together.
// SendbirdUser.kt
class SendbirdUser(
val chatUser: com.sendbird.android.user.User,
val callUser: com.sendbird.calls.User
)
// LoginHandler.kt
fun interface LoginHandler {
fun onResult(user: SendbirdUser?, e: Exception?)
}
// LoginActivity.kt
fun login(userId: String, handler: LoginHandler) {
SendbirdChat.connect(userId) { chatUser, e ->
if (chatUser == null || e != null) {
handler.onResult(null, e)
return@connect
}
SendBirdCall.authenticate(AuthenticateParams(userId)) { callUser, e ->
if (callUser == null || e != null) {
handler.onResult(null, e)
return@authenticate
}
handler.onResult(SendbirdUser(chatUser, callUser), null)
}
}
}
As written above, the Chat SDK is authenticated by using the SendbirdChat.connect() method and the Calls SDK is authenticated by using the SendBirdCall.authenticate() method.
Step 4 Log out from the Calls SDK and the Chat SDK
Push notifications services allow users to receive notifications when the app is in the background. Before utilizing this feature, you need to register appropriate push tokens to Sendbird server. To turn on this feature, go to Settings > Chat > Push notifications in your dashboard.
In your FirebaseMessagingService.kt instance, save the push token from onNewToken() method.
// FirebaseMessagingService.kt
fun registerPushToken(token: String, handler: (e: Exception?) -> Unit) {
SendbirdChat.registerPushToken(token) { status, e ->
if (e != null) {
handler(e)
return@registerPushToken
}
SendBirdCall.registerPushToken(token, false) { e ->
handler(e)
}
}
}
For integration between the Calls and Chat services, the Calls SDK provides a specific option when dialing. You can provide the group channel URL to a DialParams object of Sendbird Calls as shown below:
val dialParams = DialParams(CALLEE_ID).setSendBirdChatOptions(
SendBirdChatOptions().setChannelUrl(CHANNEL_URL)
)
val call = SendBirdCall.dial(dialParams) { directCall, e ->
if (e == null) { // dial success
...
}
}
When a group channel URL is provided to the DialParams object, messages containing call information such as calling status and duration will be automatically sent to the channel when the call starts and ends.
The messages will contain call information in the plugins field of the BaseMessage instance which can be used to properly show information about the calls.
The sample app for Calls integration to Chat is built based on Sendbird Chat. In the Chat sample app, every chat message is associated with a specific type of instance from the RecyclerView.ViewHolder class. With different types of messages such as user message, file message, and admin message, different view types of ViewHolder class are shown to the user, offering a more intuitive user experience.
For UI components for Calls integration to Chat, use the list_item_group_chat_call_me.xml layout resource file which will show the UI component like the following when users make or receive a call:
An example of a call message you can create is demonstrated in the following adapter class file in the sample app: GroupChatAdapter.java.
To create similar UI components for Calls integration to Chat, follow the steps below:
In Android Studio, go to res > layout and open the context menu.
Select New > Layout Resource File.
In the new resource file for custom layout, add image view and text view within linear layout to display icon and message. In the sample app, card view is used to display a call message.
You can integrate the custom layouts to integrate Sendbird Calls to the group channel fragment of Chat.
Customize your ViewHolder class to initialize and bind UI components by extending the RecyclerView.ViewHolder class.
public MyCallHolder(View itemView) {
super(itemView);
callText = (TextView) itemView.findViewById(R.id.text_group_chat_call);
callImage = (ImageView) itemView.findViewById(R.id.image_call);
dateText = (TextView) itemView.findViewById(R.id.text_group_chat_date);
timeText = (TextView) itemView.findViewById(R.id.text_group_chat_time);
messageStatusView = itemView.findViewById(R.id.message_status_group_chat);
// Dynamic padding that can be hidden or shown based on whether the message is continuous.
padding = itemView.findViewById(R.id.view_group_chat_padding);
}
void bind(Context context, final UserMessage message, GroupChannel channel, boolean isContinuous, boolean isNewDay, final OnItemClickListener clickListener, final OnItemLongClickListener longClickListener, final int position){
...
}
To show the registered UI components, you have to identify which messages are associated with the Calls SDK.
The BaseMessage class from Sendbird Chat SDK has a field called plugins where you can store additional information to default values. The key-value plugins are delivered as [String: String] map.
When a call is made from the Calls SDK, the plugin field of a message associated with the call will contain the following information: vendor: sendbird, type : call.
Then, for the messages that have these fields, show the UI component created.
The following can be used to identify a call message and specify the type of UI by adding it in the RecyclerView.getItemViewType() of the GroupChatAdapter.java file from the Chat sample:
fun isCallMessage(message: BaseMessage): Boolean {
if (message !is UserMessage) {
return false
}
val plugins = message.plugins
if (plugins.isNullOrEmpty()) {
return false
}
return plugins.any { it.vendor == "sendbird" && it.type == "call" }
}
A bind method of the customizable ViewHolder class shown below demonstrates a way to extract specific information from the plugin about Sendbird Calls.
The detail of a call plugin could be parsed into the CallInfo class as below.
The call activity provides events for users such as ending a call, muting or unmuting the microphone, or offers local and remote video views on a user’s screen.
Calls integration to Chat can provide a seamless user experience by allowing users to initiate a new call directly from a channel by tapping the messages that contain call information.
First, declare the OnItemClickListener interface in the GroupChatAdapter.java file.
In the bind method of the ViewHolder class, register a click listener for the group chat view fragment and implement the onClick() method like the following: