自定义消息 包含 list 数组
公司产品越来越复杂,业务线也不断的增加,项目中聊天模块使用了融云的 sdk ,但是 sdk 中自带的消息类型有限,不能满足产品需要的多张图片布局的消息类型,只能自定义一个,但是发现没有包含 list 的消息例子,实现起来有点繁琐,这边给大家参考下。
————消息体—————
新建一自定义消息类,继承 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() { } } }
大家可以当一个参考。