融云自定义消息注意事项
来源:互联网 发布:三菱人机界面怎么编程 编辑:程序博客网 时间:2024/06/05 06:37
需要在聊天中加入自定义的消息,比如旅游图片,旅游景点,等等,这个时候融云集成好的一些消息体已经不够用,官方文档上已经说明了如何去自定义,当然啦,相信你们肯定会先找一找有没有现成写好的,抱歉,你还是看文档吧,这里我写一下我遇到的坑,拿出一个我写的例子:
如同集成文档一样,需要先自定义消息体
package com.xxx.xxx.CustomizeRongIM;import android.os.Parcel;import org.json.JSONException;import org.json.JSONObject;import java.io.UnsupportedEncodingException;import io.rong.common.ParcelUtils;import io.rong.common.RLog;import io.rong.imlib.MessageTag;import io.rong.imlib.model.MentionedInfo;import io.rong.imlib.model.MessageContent;import io.rong.imlib.model.UserInfo;/** * Created by Obl on 2017/5/9. * com.xxx.xxx.CustomizeRongIM */@MessageTag(value = "XX:XXXXX", flag = MessageTag.ISCOUNTED | MessageTag.ISPERSISTED)public class CustomizeJoinMessage extends MessageContent { public CustomizeJoinMessage() { } //给消息赋值。 public CustomizeJoinMessage(Parcel in) { setUserInfo(ParcelUtils.readFromParcel(in, UserInfo.class)); setMentionedInfo(ParcelUtils.readFromParcel(in, MentionedInfo.class)); } /** * 将类的数据写入外部提供的 Parcel 中。 * * @param dest 对象被写入的 Parcel。 * @param flags 对象如何被写入的附加标志。 */ @Override public void writeToParcel(Parcel dest, int flags) { ParcelUtils.writeToParcel(dest, this.getUserInfo()); ParcelUtils.writeToParcel(dest, this.getMentionedInfo()); } public CustomizeJoinMessage(byte[] data) { String jsonStr = null; try { jsonStr = new String(data, "UTF-8"); } catch (UnsupportedEncodingException var5) { var5.printStackTrace(); } try { JSONObject e = new JSONObject(jsonStr); if (e.has("user")) { this.setUserInfo(this.parseJsonToUserInfo(e.getJSONObject("user"))); } if (e.has("mentionedInfo")) { this.setMentionedInfo(this.parseJsonToMentionInfo(e.getJSONObject("mentionedInfo"))); } } catch (JSONException var4) { RLog.e("CustomizeJoinMessage", jsonStr + "JSONException " + var4.getMessage()); } } /** * 读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。 */ public static final Creator<CustomizeJoinMessage> CREATOR = new Creator<CustomizeJoinMessage>() { @Override public CustomizeJoinMessage createFromParcel(Parcel source) { return new CustomizeJoinMessage(source); } @Override public CustomizeJoinMessage[] newArray(int size) { return new CustomizeJoinMessage[size]; } }; /** * 描述了包含在 Parcelable 对象排列信息中的特殊对象的类型。 * * @return 一个标志位,表明Parcelable对象特殊对象类型集合的排列。 */ public int describeContents() { return 0; } @Override public byte[] encode() { JSONObject jsonObj = new JSONObject(); try { if (this.getJSONUserInfo() != null) { jsonObj.putOpt("user", this.getJSONUserInfo()); } if (this.getJsonMentionInfo() != null) { jsonObj.putOpt("mentionedInfo", this.getJsonMentionInfo()); } } catch (JSONException var4) { RLog.e("CustomizeJoinMessage", "JSONException " + var4.getMessage()); } try { return jsonObj.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException var3) { var3.printStackTrace(); return null; } }}
这里需要注意的是enCode 和 带有byte 的构造方法一定要写,,查看源码你会发现每个消息都含有这几个方法。
package com.xxx.xxx.CustomizeRongIM;import android.content.Context;import android.text.Spannable;import android.text.SpannableString;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import com.xxx.xxx.R;import com.xxx.xxx.event.AddNewOneEvent;import com.xxx.xxx.utils.DensityUtils;import org.greenrobot.eventbus.EventBus;import io.rong.imkit.model.ProviderTag;import io.rong.imkit.model.UIMessage;import io.rong.imkit.widget.provider.IContainerItemProvider;/** * Created by Obl on 2017/5/9. * com.xxx.xxx.CustomizeRongIM */@ProviderTag(messageContent = CustomizeJoinMessage.class, showPortrait = false, centerInHorizontal = true, showSummaryWithName = false)public class CustomizeJoinMessageItemProvider extends IContainerItemProvider.MessageProvider<CustomizeJoinMessage> { class ViewHolder { LinearLayout llJoin; } @Override public View newView(Context context, ViewGroup group) { View view = LayoutInflater.from(context).inflate(R.layout.item_customize_join_message, group, false); ViewHolder holder = new ViewHolder(); holder.llJoin = (LinearLayout) view.findViewById(R.id.llJoin); holder.llJoin.getLayoutParams().width = DensityUtils.getScreenWidth(context); view.setTag(holder); return view; } @Override public void bindView(View view, int position, CustomizeJoinMessage content, UIMessage message) { } @Override public Spannable getContentSummary(CustomizeJoinMessage data) { return new SpannableString("创建家庭组成功"); } @Override public void onItemClick(View view, int i, CustomizeJoinMessage customizeBPMessage, UIMessage uiMessage) { EventBus.getDefault().post(new AddNewOneEvent()); } @Override public void onItemLongClick(View view, int i, CustomizeJoinMessage customizeBPMessage, UIMessage uiMessage) { }}
如果你比较懒,,你可以去看看
这里已经写好了很多原生的,,你可以修修改改,,
对了,别忘记注册在init()之后
阅读全文
1 0
- 融云自定义消息注意事项
- 融云自定义消息通知栏显示
- 融云利用自定义消息的方式发送自定义表情
- 关于融云的自定义消息的设置
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- 自定义消息
- IOS 从网上下载的项目,运行时出现“The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update y”
- Android系统音视频架构
- java基础提高篇--final关键字
- [李景山php] 深入理解PHP内核[读书笔记]--第四章:函数的实现 --函数的参数-1
- 使用jmc监控远程tomcat
- 融云自定义消息注意事项
- Calendar获取上周周一至本周一的日期
- Java的发射机制
- Android的自定义适配器实现活动中的一些资源方法,比如Toast方法弹出提示
- 只会增删查改?你还缺个数据库管理工具!
- Oracle使用Mybatis实现批量插入数据(Oracle)
- android 跳转到应用权限设置界面
- UVC 摄像头驱动(三)配置摄像头,实时数据采集
- 解决ScrollView嵌套RecyclerView的冲突,非常简单,只需要3个步骤!