自定义消息 包含 list 数组

自定义消息 包含 list 数组

公司产品越来越复杂,业务线也不断的增加,项目中聊天模块使用了融云的 sdk ,但是 sdk 中自带的消息类型有限,不能满足产品需要的多张图片布局的消息类型,只能自定义一个,但是发现没有包含 list 的消息例子,实现起来有点繁琐,这边给大家参考下。
--------消息体----------

  1. 新建一自定义消息类,继承 MessageContent

2.实现 encode() 方法,该方法的功能是将消息属性封装成 json 串,再将 json 串转成 byte 数组,该方法会在发消息时调用
注意:要在这个方法里面加上这句话用来携带用户信息
if (getJSONUserInfo() != null){
jsonObj.putOpt("user", getJSONUserInfo());
}

3.覆盖父类的 MessageContent(byte[] data) 构造方法,该方法将对收到的消息进行解析,先由 byte 转成 json 字符串,再将 json 中内容取出赋值给消息属性。
注意:要在这个方法里面加上这句话用来解析携带用户信息
if (jsonObj.has("user"))
setUserInfo(parseJsonToUserInfo(jsonObj.getJSONObject("user")));

4.MessageContent 已实现 Parcelable 接口,自定义消息类也需要实现 Parcelable

5.增加注解信息 :注解名:MessageTag ;属性:value ,flag; value 即 ObjectName 是消息的唯一标识不可以重复,
开发者命名时不能以 RC 开头,避免和融云内置消息冲突;flag 是用来定义消息的可操作状态。
flag 值如下表:
枚举值 说明
MessageTag.NONE 为空值,不表示任何意义,发送的自定义消息不会在会话页面和会话列表中展示。
MessageTag.ISCOUNTED 表示客户端收到消息后,要进行未读消息计数(未读消息数增加 1),所有内容型消息都应该设置此值。非内容类消息暂不支持消息计数。
MessageTag.ISPERSISTED 表示客户端收到消息后,要进行存储,并在之后可以通过接口查询,存储后会在会话界面中显示。
MessageTag.STATUS 在本地不存储,不计入未读数,并且如果对方不在线,服务器会直接丢弃该消息,对方如果之后再上线也不会再收到此消息(聊天室类型除外,此类消息聊天室会视为普通消息)。

6.自定义消息应在 init 后注册 RongIM.registerMessageType(CustomizeMessage.class);
-----------自定义消息展示---------

package cn.rongcloud.demo;

import android.os.Parcel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

@MessageTag(value = "sendGoodsLocal", flag = MessageTag.ISPERSISTED)
public class SendGoodsLocalMessage extends MessageContent {
    private String content;
    private String id;
    private int typeid;
    private List<String> img =new ArrayList<>();
    private SendGoodsLocalMessage() {
    }

    public SendGoodsLocalMessage(Parcel in) {
        this.content = ParcelUtils.readFromParcel(in);
        this.id = ParcelUtils.readFromParcel(in);
        this.typeid = ParcelUtils.readIntFromParcel(in);
        this.img = ParcelUtils.readListFromParcel(in,String.class);
    }

    public SendGoodsLocalMessage(byte[] data) {
        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException var5) {
            RLog.e("GroupNotificationMessage", "UnsupportedEncodingException ", var5);
        }

        try {
            JSONObject jsonObj = new JSONObject(jsonStr);
            this.setContent(jsonObj.optString("content"));
            this.setId(jsonObj.optString("id"));
            this.setTypeid(jsonObj.optInt("typeid"));
            JSONArray jsonArray = jsonObj.optJSONArray("img");
            List<String> imgList = new ArrayList<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                imgList.add((String) jsonArray.get(i));
            }
            setImg(imgList);
//            this.setImg();
        } catch (JSONException var4) {
            RLog.e("GroupNotificationMessage", "JSONException " + var4.getMessage());
        }

    }

    public static final Creator<SendGoodsLocalMessage> CREATOR = new Creator<SendGoodsLocalMessage>() {
        public SendGoodsLocalMessage createFromParcel(Parcel source) {
            return new SendGoodsLocalMessage(source);
        }

        public SendGoodsLocalMessage[] newArray(int size) {
            return new SendGoodsLocalMessage[size];
        }
    };

    public static SendGoodsLocalMessage obtain(String content, String id, int typeid, List<String> img) {
        SendGoodsLocalMessage obj = new SendGoodsLocalMessage();
        obj.content = content;
        obj.id = id;
        obj.typeid = typeid;
        obj.img = img;
        return obj;
    }

    public String getContent() {
        return content;
    }

    public List<String> getImg() {
        return img;
    }

    public void setImg(List<String> img) {
        this.img = img;
    }

    public int getTypeid() {
        return typeid;
    }

    public String getId() {
        return id;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setTypeid(int typeid) {
        this.typeid = typeid;
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();
        try {
            jsonObj.put("content", this.content);
            jsonObj.put("id", this.id);
            jsonObj.put("typeid", this.typeid);

            JSONArray jsonArray = new JSONArray();
            for (String name : img) {
                jsonArray.put(name);
            }
            jsonObj.put("img", jsonArray);

        } catch (JSONException var4) {
            RLog.e("GroupNotificationMessage", "JSONException " + var4.getMessage());
        }
        try {
            return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException var3) {
            RLog.e("GroupNotificationMessage", "UnsupportedEncodingException ", var3);
            return null;
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, this.content);
        ParcelUtils.writeToParcel(dest, this.id);
        ParcelUtils.writeToParcel(dest, this.typeid);
        ParcelUtils.writeToParcel(dest, this.img);
    }
}

package cn.rongcloud.demo;


import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;

import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.provider.IContainerItemProvider;

@ProviderTag(
        messageContent = SendGoodsLocalMessage.class,
        showPortrait = false,
        centerInHorizontal = true,
        showProgress = false,
        showSummaryWithName = false
)
public class SendGoodsLocalProvider extends IContainerItemProvider.MessageProvider<SendGoodsLocalMessage> {

    @Override
    public void bindView(View v, int i, SendGoodsLocalMessage mSendGoodsLocalMessage, UIMessage uiMessage) {
        ViewHolder holder = (ViewHolder) v.getTag();
        holder.tvGoodsName.setText(mSendGoodsLocalMessage.getContent());
        Glide.with(v.getContext()).load(mSendGoodsLocalMessage.getImg().get(0)).centerCrop().into(holder.ivCover);
    }

    @Override
    public Spannable getContentSummary(Context context, SendGoodsLocalMessage message) {
        if (message == null) {
            return null;
        }
        return new SpannableString(message.getContent());
    }


    @Override
    public String getPushContent(Context context, UIMessage message) {
        return super.getPushContent(context, message);
    }

    @Override
    public Spannable getContentSummary(SendGoodsLocalMessage mSendGoodsLocalMessage) {
        return null;
    }

    @Override
    public void onItemClick(View view, int i, SendGoodsLocalMessage mSendGoodsLocalMessage, UIMessage uiMessage) {

    }

    @Override
    public View newView(Context context, ViewGroup viewGroup) {
        View view = LayoutInflater.from(context).inflate(R.layout.rc_send_goods_message, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.tvGoodsName = view.findViewById(R.id.tvGoodsName);
        viewHolder.tvSend = view.findViewById(R.id.tvSend);
        viewHolder.ivCover = view.findViewById(R.id.ivCover);
        view.setTag(viewHolder);
        return view;
    }
    private static class ViewHolder {
        TextView tvGoodsName,tvSend;
        ImageView ivCover;
        private ViewHolder() {
        }
    }
}

大家可以当一个参考。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容