美文网首页腾讯云IM + TRTC
腾讯云IM 聊天发送自定义消息

腾讯云IM 聊天发送自定义消息

作者: 酒仙娘子 | 来源:发表于2019-05-30 11:43 被阅读0次

    发送自定义消息官网也讲的很清楚

    自定义消息是指当内置的消息类型无法满足特殊需求,开发者可以自定义消息格式,内容全部由开发者定义,IM SDK 只负责透传。另外如果需要 iOS APNs 推送,还需要提供一段推送文本描述,方便展示。自定义消息由TIMCustomElem定义,其中data存储消息的二进制数据,其数据格式由开发者定义,desc存储描述文本。一条消息内可以有多个自定义Elem,并且可以跟其他Elem混合排列,离线 Push 时叠加每个Elem的desc描述信息进行下发。

    首先发送消息的时候会有个消息  info.setMsgType(MessageInfo.MSG_TYPE_CUSTOM_FACE);

    先找到 是发送纯文本的方法:

    public static MessageInfo buildTextMessage(String message) {

    MessageInfo info =new MessageInfo();

        TIMMessage TIMMsg =new TIMMessage();

        TIMTextElem ele =new TIMTextElem();

        ele.setText(message);

        TIMMsg.addElement(ele);

        info.setExtra(message);

        info.setMsgTime(System.currentTimeMillis());

        info.setSelf(true);

        info.setTIMMessage(TIMMsg);

        info.setFromUser(TIMManager.getInstance().getLoginUser());

        info.setMsgType(MessageInfo.MSG_TYPE_TEXT);

    return info;

    }

    想要自定义发送消息的时候就在纯文本的方法里面再添加一个官网自定义的   TIMCustomElem elem =newTIMCustomElem();

    public static MessageInfo buildTextMessage(String message, String id, String username, String headurl) {

    MessageInfo info =new MessageInfo();

        TIMMessage TIMMsg =new TIMMessage();

        TIMTextElem ele =new TIMTextElem();

        ele.setText(message);

        TIMMsg.addElement(ele);

        info.setExtra(message);

        info.setMsgTime(System.currentTimeMillis());

        info.setSelf(true);

        info.setTIMMessage(TIMMsg);

        info.setFromUser(TIMManager.getInstance().getLoginUser());

        info.setMsgType(MessageInfo.MSG_TYPE_TEXT);

       

       上面添加纯文本的消息没动,直接再添加下面的自定义消息,我是以JSON的格式提交的 ,这种格式可以自己和后台配置人员进行沟通用哪一种,再添加。

       TIMCustomElem customElem =new TIMCustomElem();

        JSONObject jsonObject =new JSONObject();

        try {

    jsonObject.put("id", id);

            jsonObject.put("nickname", username);

            jsonObject.put("headurl", headurl);

            customElem.setData(jsonObject.toString().getBytes());

            TIMMsg.addElement(customElem);

        }catch (JSONException e) {

    }

    return info;

    }

    到这自定义发送消息,以及格式完成了发送步骤,下一步就是如何获取自定义消息的方法。

    在获取消息TIMMessage  解析的时候 getElement  一层一层的去解析获取数据

    自定义发送  然后再去取自定义的里面的数据出来再解析一下JSON  我的是data对象里面包了自己定义的字段,所以取的时候就先解析获取到data对象再取里面的自定义字段就可以了 “nickname” 和"headurl" 就是我自己定的字段名称。

    if (timMsg.getElementCount() >1 &&timMsg.getElement(1)instanceof TIMCustomElem) {

    try {

    jsonObject =new JSONObject(new String(((TIMCustomElem)timMsg.getElement(1)).getData(), "UTF-8"));

            chatHolder.userName.setText(jsonObject.get("nickname").toString());

            GlideEngine.chatHeader(TUIKit.getAppContext(), jsonObject.get("headurl").toString(), chatHolder.userPhoto);

        }catch (JSONException e) {

    e.printStackTrace();

        }catch (UnsupportedEncodingException e) {

    e.printStackTrace();

        }

    }

    其实官网上面也讲的很清楚很清楚了。还是比较简单的,没什么难的。唯一一点可能就是要和后台人员商量好定义对应的格式,要不然可能就会解析错误已经发送格式失败之类的。

    相关文章

      网友评论

        本文标题:腾讯云IM 聊天发送自定义消息

        本文链接:https://www.haomeiwen.com/subject/bvemtctx.html