网易云信实现自定义消息

*在接入的网易云信中加入自定义消息,以便达到更好的展示效果;经多次浏览文档,参考他人提供资料,整理出自定义消息的方法*
1- 我们在CustomAttachmentType 先定义一个自定义消息的类型

public interface CustomAttachmentType {    
     // 多端统一 
     int Guess = 1;  
     int SnapChat = 2;    
     int Sticker = 3;    
     int RTS = 4;    
     int Accede = 5; //自定义
     }

2-我们先定义一个自定义消息附件的基类,负责解析你的自定义消息的公用字段,比如类型等注意: 实现 MsgAttachment 接口的成员都要实现 Serializable。(这个是云信已经写好的demo);

public abstract class CustomAttachment implements MsgAttachment {
    protected int type;
    CustomAttachment(int type) {
        this.type = type;
    }
    public void fromJson(JSONObject data) {
        if (data != null) {
            parseData(data);
        }
    }
    @Override
    public String toJson(boolean send) {
        return CustomAttachParser.packData(type, packData());
    }
    public int getType() {
        return type;
    }
    protected abstract void parseData(JSONObject data);
    protected abstract JSONObject packData();
}

3-我们自定义的消息类要继承这个类,实现我们想要的功能
特别要注意的是这里的成员变量都要实现 Serializable

public class QueryAttachment extends CustomAttachment implements Serializable{

    public String url;
    //这个图片没使用,在布局里面放张默认图片
    public String title;
    public String status;
    public String id;
    public QueryAttachment() {
        super(CustomAttachmentType.Accede);
    }
    public QueryAttachment(String str) {
        this();
    }
    @Override
    protected void parseData(JSONObject data) {
        title=data.getString("title");
        url=data.getString("url");
        id=data.getString("id");
        status=data.getString("status");
    }

    @Override
    protected JSONObject packData() {
        JSONObject data = new JSONObject();
        data.put("title",title);
        data.put("url",url);
        data.put("id",id);
        data.put("status",status);
        return data;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

4-实现自定义消息的附件解析器。CustomAttachParser 这个类也是云信提供了的,我们只要注册解析就好

public class CustomAttachParser implements MsgAttachmentParser {
//类里面 
    case CustomAttachmentType.Accede:   
         attachment = new QueryAttachment();   
         break;

5-将自定义消息展示UI上并增加点击处理事件,这里的xml我就不在给出了,大家可以自行定义;

public class MsgViewHolderQuery extends MsgViewHolderText {

    private QueryAttachment attachment;
    private ImageView image;
    private TextView tvTitle;
    private TextView tvContent;
    private LinearLayout line;

    @Override
    protected int getContentResId() {
        return R.layout.msg_query;
    }

    @Override
    protected void inflateContentView() {
        line = (LinearLayout) findViewById(R.id.msg_line);
        image = (ImageView) findViewById(R.id.msg_img);
        tvTitle = (TextView) findViewById(R.id.msg_title);
        tvContent = (TextView) findViewById(R.id.msg_context);
    }

    @Override
    protected void bindContentView() {

        attachment = (QueryAttachment) message.getAttachment();
        tvTitle.setText(attachment.getTitle());
        if(!userData.getId().equals(attachment.getId())) {
            line.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                //自行增加点击事件,云信还提供了数据信息来源的判断;

                }
            });
        }
    }
//    //若是要自己修改气泡背景
//    // 当是发送出去的消息时,内容区域背景的drawable id
//    // @Override
//    protected int rightBackground() {
//        return com.netease.nim.uikit.R.drawable.nim_message_item_right_selector;
//    }

}

6-设置数据,发送自定义消息

     QueryAttachment attachment = new QueryAttachment();
            attachment.setTitle("数据");
            attachment.setUrl("数据");
            attachment.setStatus("数据");
            attachment.setId(data.getStringExtra("id"));
            IMMessage   message = 
            MessageBuilder.createCustomMessage(getAccount(), 
            getSessionType(), attachment);
            sendMessage(message);

7- 将该附件解析
器注册到 SDK 中。为了保证生成历史消息时能够正确解析自定义附件,注册一般应放在 Application 的 onCreate 中完成

NIMClient.getService(MsgService.class).registerCustomAttachmentParser(new CustomAttachParser()); // 监听的注册,必须在主进程中。

如果还有什么疑问可以私信我;
end;

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值