我刚开始在 Android 上使用 SendBird。我想进行 1 合 1 聊天,所以我将用户连接到 SendBird,设置处理程序,创建通道并发送消息,但是,onMessageReceived 不起作用。请看下面的代码:
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}")
}
})
}
})