Android极光Im,1对1单聊

·  阅读 548

项目中需要实现一个客服功能。

注册和登录我就不做过多的描述了。     


声明:

var adapter: ChatAdapter? = null

var animationDrawable: AnimationDrawable? = null

var chatItemlist: MutableList<ChatItem> = mutableListOf()

var conversation: Conversation? = null

var account_target = ""//聊天对象的id
 复制代码


初始化:

注册接收,创建一个单人的聊天室,然后接收本地聊天记录

JMessageClient.registerEventReceiver(this)
conversation = JMessageClient.getSingleConversation(account_target)if (conversation == null) {
    conversation = Conversation.createSingleConversation(account_target)
}
if (conversation?.allMessage != null) {

    for (bean in conversation?.allMessage!!.toMutableList()) {

        if (bean.direct == MessageDirect.send) {
            if (bean.contentType == ContentType.text) {
                chatItemlist.add(ChatItem(ChatItem.SEND, (bean.getContent() as TextContent).text, usericon, bean.createTime))
            } else if (bean.contentType == ContentType.image) {
                recordPaths.add((bean.getContent() as ImageContent).localThumbnailPath)
                chatItemlist.add(ChatItem(ChatItem.SENDIMAGE, (bean.getContent() as ImageContent).localThumbnailPath, usericon, bean.createTime))
            } else if (bean.contentType == ContentType.voice) {
                chatItemlist.add(ChatItem(ChatItem.SENDAUDIO, bean.getContent().toString(), usericon, (bean.getContent() as VoiceContent).duration, (bean.getContent() as VoiceContent).localPath, bean.createTime))

            }

        } else {
            if (bean.contentType == ContentType.text) {
                chatItemlist.add(ChatItem(ChatItem.RECEIVE, (bean.getContent() as TextContent).text, "", bean.createTime))
            } else if (bean.contentType == ContentType.image) {
                recordPaths.add((bean.getContent() as ImageContent).localThumbnailPath)
                chatItemlist.add(ChatItem(ChatItem.RECEIVEIMAGE, (bean.getContent() as ImageContent).localThumbnailPath, "", bean.createTime))
            } else if (bean.contentType == ContentType.voice) {
                if (bean.haveRead() == true) {
                    chatItemlist.add(ChatItem(ChatItem.RECEIVEAUDIO, "0", usericon, (bean.getContent() as VoiceContent).duration, (bean.getContent() as VoiceContent).localPath, bean.createTime))
                } else {
                    message = bean
                    Log.e("读了没本地", bean.haveRead().toString())
                    chatItemlist.add(ChatItem(ChatItem.RECEIVEAUDIO, "1", usericon, (bean.getContent() as VoiceContent).duration, (bean.getContent() as VoiceContent).localPath, bean.createTime))

                }

            }

复制代码


在线接收消息类:

fun onEvent(event: MessageEvent) {
if (event.message.direct == MessageDirect.send) {
 //判断是发送者还是接受者 还是在处理消息类型
}
}复制代码


发送文字消息:

btn_send1.setOnClickListener {
    if (TextUtils.isEmpty(et_content.text.toString())) {
        Toast.makeText(this@ChatActivity, "请输入发送内容", Toast.LENGTH_SHORT).show()

        return@setOnClickListener
    }

    var content = et_content.text.toString()
    var textContent = TextContent(content)
    var m = conversation?.createSendMessage(textContent)
    m?.setOnSendCompleteCallback(object : BasicCallback() {
        override fun gotResult(p0: Int, p1: String?) {
            if (p0 == 0) {
                var currentTime = System.currentTimeMillis()
                chatItemlist.add(ChatItem(ChatItem.SEND, content, usericon, currentTime))
                adapter?.notifyDataSetChanged()
                jg_details_recy.scrollToPosition(chatItemlist.size - 1)
            } else {
                Log.e("发送文字消息", p0.toString() + p1)
            }
        }
    })
    JMessageClient.sendMessage(m)
    et_content.setText("")复制代码

先写到这里 后续详细更新


分类:
Android
标签:
分类:
Android
标签:
收藏成功!
已添加到「」, 点击更改