I just started using SendBird on Android. I want to make 1-in-1 chat, so I connected users to SendBird, set the handler, created channel and sent a message, however, the onMessageReceived is not working. See the code below:
var handler = object : SendBird.ConnectHandler{
override fun onConnected(p0: User?, p1: SendBirdException?) {
SendBird.addChannelHandler(UNIQUE_HANDLER_ID, object : SendBird.ChannelHandler() {
override fun onMessageReceived(p0: BaseChannel?, p1: BaseMessage?) {
Log.d(TAG, "addChannelHandler - onMessageReceived - messageId ${p1?.messageId}")
Log.d(TAG, "addChannelHandler - onMessageReceived - channelUrl ${p1?.channelUrl}")
if (p1 is UserMessage) {
Log.d(TAG, "addChannelHandler - onMessageReceived - messageId ${p1?.message}")
}
}
})
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
SendBird.connect("222222", handler)
SendBird.connect("111111", object : SendBird.ConnectHandler {
override fun onConnected(p0: User?, p1: SendBirdException?) {
Log.d(TAG, " SendBird.connect - userID - " + p0?.userId)
var list = mutableListOf("111111", "222222")
var params = GroupChannelParams()
params.setDistinct(true)
.addUserIds(list)
GroupChannel.createChannel(params, object : GroupChannel.GroupChannelCreateHandler {
override fun onResult(p0: GroupChannel?, p1: SendBirdException?) {
groupChannel?.sendUserMessage("Hello", object : BaseChannel.SendUserMessageHandler {
override fun onSent(p0: UserMessage?, p1: SendBirdException?) {
Log.d(TAG, "sendUserMessage - onSent - ${p0?.message}")
}
})
}
})
The SendBird Android and iOS SDKs are intended for client-side code on mobile. Only one user connection should be made per client, not multiple user connections on the client-side mobile app.
I recommend testing on two separate devices/emulators to simulate two users messaging in your application. The code should have one handler that adds the ChannelHandler for the current Android user that calls onMessageReceived(p0: BaseChannel?, p1: BaseMessage?). Your application could have a list of users where user 111111 can select user 222222 and send a message to that user. On select, the app would create a distinct group channel for user 111111 (the current user) and user 222222 (the selected user(s)). Then, user 111111 could send a message in that channel.
For a code sample, please feel free to download our Android Chat Sample here: https://github.com/smilefam/Sendbird-Android
We would love to learn more about your use case and to help you get this working. Please email [email protected] for further support and reference "StackOverflow" in the subject line.
-- Janna, Solutions Engineer @ SendBird