需求:在消息列表显示其他内容入口,如心动列表,最近来访,小助 手等,点击跳到相应界面
效果图:
如效果图所示:小纸条,心动列表等都是自定义的会话。
实现做法有两个:
- app服务器创对应的targetId发相应内容给客户端
- 客户端本地自己创建会话
我推荐用客户端本地自己创建,因为
- 节省融云每天的发信息流量数,特别app用户多的,这成本就大了
- 用户如果换手机也是能正常显示的
- 如果出现问题,排查问题或者还想在上面操作,会方便很多
反正我是客户端能做的事,尽量不要接口弄
步骤1:创建会话
关键方法:Conversation.obtain
示例代码:
public static Conversation createConversation(String targetId, int title) {
return Conversation.obtain(Conversation.ConversationType.SYSTEM, targetId, MyApplication.getInstance().getString(title));
}
参数说明:targetId任意(最好与ios协定好),title如上面的心动列表。
至于采用Conversation.ConversationType.SYSTEM是为了和private普通用户聊天区别开
步骤2:添加会话到会话列表中
关键方法:重写getConversationList
示例代码:
`public class ConversationListFragmentEx extends ConversationListFragment {
//心动
private boolean isLove;
//小纸条
private boolean isNotes;
//邀请
private boolean isAuthInvite;
//婚恋
private boolean isMatch;
//微信下载
private boolean isWxDown;
//最近来访
private boolean isRecentVisit;
@Override
public void getConversationList(Conversation.ConversationType[] ctc, final IHistoryDataResultCallback> callback,boolean isLoadMore) {
RongIMClient.getInstance().getConversationList(new RongIMClient.ResultCallback>() {
public void onSuccess(List cs) {
if (getActivity() != null && !getActivity().isFinishing()) {
if (callback != null) {
ArrayList csl = new ArrayList();
if (null != cs && cs.size() > 0)
for (Conversation c : cs) {
if (c == null) continue;
String targetId = c.getTargetId();
if (targetId.contains(ImType.BUSINESS) || isNoMyCus(targetId, c.getConversationType()))
continue;
if (targetId.contains(ImType.MYCUS)) dealFlag(targetId);
//添加需要的会话
csl.add(c);
}
//自定义会话列表的处理
if (!isLove)
csl.add(ImUtils.createConversation(ImType.HEART_BEAT, R.string.im_love_name));
if (!isNotes)
csl.add(ImUtils.createConversation(ImType.SLIP, R.string.im_notes_name));
if (!isWxDown)
csl.add(ImUtils.createConversation(ImType.WECHAT_DOWNLOAD, R.string.im_wx_name));
if (!isAuthInvite)
csl.add(ImUtils.createConversation(ImType.AUTH_INVITE, R.string.im_invite_auth_name));
if(InfoUtils.isMarriageIsOpen()){
if (!isRecentVisit)
csl.add(ImUtils.createConversation(ImType.RECENT_VISIT, R.string.im_recents_name));
// 3.0版本后是否显示“红娘牵线服务”
if (!isMatch && InfoUtils.getMarriageEntryShow())
csl.add(ImUtils.createConversation(ImType.MATCH_SERVICE, R.string.im_maker));
}
if(!isGreet){
csl.add(ImUtils.createConversation(ImType.GREET_MSG,R.string.im_greet_msg));
}
callback.onResult(csl);
}
}
}
public void onError(RongIMClient.ErrorCode e) {
if (callback != null) {
callback.onError();
}
}
}, ctc);
}
private void dealFlag(String targetId) {
switch (targetId) {
case ImType.HEART_BEAT:
isLove = true;
break;
case ImType.SLIP:
isNotes = true;
break;
case ImType.WECHAT_DOWNLOAD:
isWxDown = true;
break;
case ImType.AUTH_INVITE:
isAuthInvite = true;
break;
case ImType.RECENT_VISIT:
isRecentVisit = true;
break;
case ImType.MATCH_SERVICE:
isMatch = true;
break;
}
}
}
解析:添加自定义的会话到会话列表集合中,做下标志,防止添加多次。可能有一两个变量没有,删掉即可,重在理解
步骤3:处理会话显示
关键:自定义Provider
示例代码:
@ConversationProviderTag(conversationType = "system", portraitPosition = 3)
public class MyCusConversationProvider extends SystemConversationProvider {
@Override
public void bindView(View view, int position, UIConversation uc) {
ViewHolder vh = (ViewHolder) view.getTag();
vh.tv_content.setTextColor(Color.parseColor("#aaaaaa"));
switch (uc.getConversationTargetId()) {
case ImType.HEART_BEAT:
//心动
vh.aiv_img.setResource("", R.mipmap.ic_record);
vh.tv_name.setText(R.string.im_love_name);
vh.tv_content.setText(R.string.im_love_content);
vh.tv_conversation_time.setText("");
break;
case ImType.SLIP:
//小纸条
vh.aiv_img.setResource("", R.mipmap.ic_message);
vh.tv_name.setText(R.string.im_notes_name);
vh.tv_content.setText(R.string.im_notes_content);
vh.tv_conversation_time.setText("");
break;
case ImType.WECHAT_DOWNLOAD:
//微信下载
vh.aiv_img.setResource("", R.mipmap.ic_record1);
vh.tv_name.setText(R.string.im_wx_name);
vh.tv_content.setText(R.string.im_wx_content);
vh.tv_conversation_time.setText("");
break;
case ImType.AUTH_INVITE:
//邀请认证
vh.aiv_img.setResource("", R.mipmap.ic_invitations);
vh.tv_name.setText(R.string.im_invite_auth_name);
vh.tv_content.setText(R.string.im_invite_auth_content);
vh.tv_conversation_time.setText("");
break;
case ImType.RECENT_VISIT:
//最近来访
vh.aiv_img.setResource("", R.mipmap.ic_im_visit);
vh.tv_name.setText(R.string.im_recents_name);
vh.tv_content.setText(R.string.im_recents_content);
vh.tv_conversation_time.setText("");
break;
case ImType.MATCH_SERVICE:
//红娘牵线服务
vh.aiv_img.setResource("", R.mipmap.ic_matcher_service);
vh.tv_name.setText(R.string.im_maker);
vh.tv_content.setText(R.string.im_maker_content);
vh.tv_conversation_time.setText("");
break;
case ImType.GREET_MSG:
//对你感兴趣的人发来消息
vh.aiv_img.setResource("", R.mipmap.ic_im_greet_conversation);
vh.tv_name.setText(R.string.im_greet_msg);
vh.tv_content.setText(R.string.im_greet_msg_default);
vh.tv_conversation_time.setText("");
break;
case ImType.KEFU_ID:
//客服小助手
vh.aiv_img.setAvatar(uc.getIconUrl());
vh.tv_name.setText(uc.getUIConversationTitle());
vh.tv_content.setText(uc.getConversationContent().toString());
vh.tv_conversation_time.setText(RongDateUtils.getConversationListFormatDate(uc.getUIConversationTime(), view.getContext()));
break;
default:
//默认显示接口后台设置的,比如活动通知
vh.aiv_img.setAvatar(uc.getIconUrl());
vh.tv_name.setText(uc.getUIConversationTitle());
vh.tv_content.setText(uc.getConversationContent().toString());
vh.tv_conversation_time.setText(RongDateUtils.getConversationListFormatDate(uc.getUIConversationTime(), view.getContext()));
break;
}
}
@Override
public View newView(Context context, ViewGroup viewGroup) {
View v = LayoutInflater.from(context).inflate(R.layout.im_item_conversation_list, (ViewGroup) null);
ViewHolder vh = new ViewHolder();
vh.aiv_img = v.findViewById(R.id.aiv_img);
vh.tv_name = v.findViewById(R.id.tv_name);
vh.tv_content = v.findViewById(R.id.tv_content);
vh.unread = v.findViewById(R.id.tv_unread_count);
vh.tv_conversation_time = v.findViewById(R.id.tv_conversation_time);
v.setTag(vh);
return v;
}
protected class ViewHolder {
public AsyncImageView aiv_img;
public TextView tv_name;
public TextView tv_content;
public TextView unread;
public TextView tv_conversation_time;
}}
代码解析:因为我创的是system会话类别,所有需要@ConversationProviderTag(conversationType = “system”, portraitPosition = 3)
步骤4:注册Provider
RongIM.getInstance().registerConversationTemplate(new MyCusConversationProvider());
步骤5:点击事件
onConversationClick方法中获取targetId判断跳转即可
至此自定义列表会话就完成了,