1

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}")
                        }
                    })



                }
            })
sunflower20
  • 469
  • 3
  • 8
  • 20

1 Answers1

2

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 support@sendbird.com for further support and reference "StackOverflow" in the subject line.

-- Janna, Solutions Engineer @ SendBird

jannamcl
  • 91
  • 3
  • Thank you very much for the answer. Would you please also tell what would be the guideline in the case when user1 sends message to user2, but user2 is not connected. – sunflower20 Jul 18 '18 at 07:32
  • Are you currently using FCM for push notification support? When user2 is not connected, you can send a push to prompt the user to return to the app. Here is our guide on Android push notifications for more details: https://docs.sendbird.com/android/push_notifications – jannamcl Jul 20 '18 at 17:52
  • Thank you Janna, I just wrote an email to the support team. Unfortunately, I can't copy it here. The main idea is to build a 1-to-1 chat so that users are able to have separate chats for different topics, ex. me and you have chat about work, another chat about sports, etc.. I tried this solution: set distinct to be false, gave topic as a customType and cached urls. So everytime i need a chat instance i check if I have chat with user1, user2 and topic1, if yes I get the chat by url, if no I create new one and again store the url. Is there any better solution to this? Am i missing smth? – sunflower20 Jul 24 '18 at 10:15
  • @JannaMcLaughlin I am implementing SendBird in my Xamarin android app, Every Thing is fine, but i am facing issue in showing Last Seen, Online status for those user with whom i have initiated chat because of I am getting List of person with whom i have chat as a MessagingChannel and there is not last seen and online property in MessagingChannel Object. Please help me. – Naveen Tiwari Aug 11 '18 at 10:08