会话管理

OpenIMSDK 通过会话来管理聊天场景,具体可以参加demo工程ConversationSampleHelper

会话管理主要对象是IYWConversationService,通过imCore.getConversationService()来获取
IMCore的对象获取,请参考这里

获取最近会话列表

// 获取会话管理类
IYWConversationService conversationService = imCore.getConversationService();
// 添加会话列表变更监听
conversationService.addConversationListener(new YWConversationListener() {

    @Override
    public void onItemUpdated() {
        // 会话列表有变更,如果ISV开发者拿list来做ui展现用,可以直接调用BaseAdapter.notifyDataSetChanged()即可。
    }
});
//获取最近会话列表
List<YWConversation> list = conversationService.getConversationList();

会话置顶

//获取与某个聊天对象的会话记录,userId:表示聊天对象id
YWConversation conversation = imCore.getConversationService()
            .getConversation(userId);
//将该会话置顶,该方法必须在UI主线程调用
imCore.getConversationService().setTopConversation(conversation);
//取消置顶,该方法必须在UI主线程调用
imCore.getConversationService().removeTopConversation(conversation);

删除会话

//获取会话管理类
IYWConversationService conversationService = imCore.getConversationService();
// 清空所有会话
conversationService.deleteAllConversation();
// 删除某一条会话记录
conversationService.deleteConversation(conversation);

获取当前会话的最近一条消息

//获取与某个聊天对象的会话记录,userId:表示聊天对象id
YWConversation conversation = imCore.getConversationService()
            .getConversation(userId);
//通常在消息列表中需要显示每个会话的最近一条消息的内容,发送时间等信息,此时可以调用该方法获取最近一条消息
YWMessage message = conversation.getLastestMessage();

标记会话已读

// 获取会话管理类
IYWConversationService conversationService = imCore.getConversationService();
// 将所有会话标记为已读
conversationService.markAllReaded();
// 将某一条会话标记为已读
conversationService.markReaded(conversation);

获取与某个用户的会话

// 该接口可能返回空,请确认已经创建了与该对象的会话
YWConversation conversation = imCore.getConversationService()
        .getConversation(userId);

获取与某个跨站用户的会话

//获取会话管理类
IYWConversationService conversationService = imCore.getConversationService();
YWContact ywContact = YWContactFactory.createAPPContact(targetUserId, targetAppkey);
YWConversation conversation = conversationService.getConversation(ywContact);

获取跨站会话联系人

// 获取会话管理类
YWP2PConversationBody p2pBody = (YWP2PConversationBody)conversation.getConversationBody();
YWContact ywContact = p2pBody.getContact();
String appKey = ywContact.getAppKey();
String userId = ywContact.getUserId();

创建或者更新自定义会话

//创建自定义会话module
YWCustomConversationUpdateModel customConversation = new YWCustomConversationUpdateModel();
//设置自定义会话唯一标识
customConversation.setIdentity(conversationID);
//设置自定义会话的最新消息内容
customConversation.setContent(content);
//设置自定义会话的最近联系时间
customConversation.setLastestTime(new Date().getTime());
//可选,设置会话子类型,该字段的使用场景: 开发者有多个自定义会话时可以通过该字段标志自定义会话的类型
customConversation.setSubType(subtype);
//可选,设置扩展信息, SDK为开发者提供了三个扩展字段,开发者可以使用这三个扩展字段设置自己的扩展信息
customConversation.setExtraData(extraData);
customConversation.setExtraData1(extraData1);
customConversation.setExtraData2(extraData2);
//创建或者更新自定义会话,若该自定义会话不存在,则会创建该自定义会话,若已存在,则更新自定义会话
conversationService.updateOrCreateCustomConversation(mCustomConversation);

获取所有会话的总未读数

//获取所有会话的总未读数,该方法必须在UI线程调用
int totalUnreadCount = conversationService.getAllUnreadCount();

创建/获取会话对象

获取普通单聊会话

YWConversation conversation = conversationService.getConversationByUserId(userid, appkey);
//若获取到的conversation为null,则创建一个
if (conversation == null) { 
    IYWContact contact = YWContactFactory.createAPPContact(userId, targetAppkey);
    conversation = service.getConversationCreater().createConversationIfNotExist(contact);
}

获取群会话

YWConversation conversation = conversationService.getTribeConversation(tribeId);
//若获取到的conversation为null,则创建一个
if (conversation == null) { 
    conversation = service.getConversationCreater().createTribeConversation(tribeId);
}

获取客服会话

//创建客服联系人对象
EServiceContact contact = new EServiceContact(userId, groupId);
//获取客服会话
YWConversation conversation = conversationService.getConversation(contact);
//若获取到的conversation为null,则创建一个
if (conversation == null) {
    conversation = conversationService.getConversationCreater().createConversation(contact);
}

根据会话id获取会话对象

//根据会话id从会话列表获取会话,若会话列表中不存在该会话,则该方法会返回null,因此开发者调用该接口后请务必做判空处理
YWConversation conversation = conversationService.getConversationByConversationId(conversationId);

获取自定义会话

YWConversation conversation = conversationService.getCustomConversationByConversationId(conversationId);

会话草稿功能

创建会话草稿

//创建会话草稿,无参构造,需要调用draft.setContent(String),setTime(long)设置时间和内容
YWConversationDraft draft = conversation.createDraft();
//创建会话草稿,有参数构造
YWConversationDraft draft = conversation.createDraft(content, time);

获取会话草稿

YWConversationDraft draft = conversation.getConversationDraft();

设置会话草稿

conversation.setConversationDraft(conversationDraft);

删除会话草稿

//把草稿内容设置为空就可以删除草稿了
conversationDraft.setContent("");

保存草稿

conversation.saveDraft();

保存所有会话的草稿(批处理)

conversationService.saveConversationDrafts();

会话相关监听

单聊和客服消息到达监听

开发者可以通过以下接口添加单聊和客服消息到达监听,为了监听到所有消息,请在登录前添加该监听。具体使用方式可以参考DEMO LoginSampleHelper.addPushMessageListener().

//添加消息到达监听,参数类型为IYWP2PPushListener
conversationService.addP2PPushListener(listener);
//移除消息到达监听,参数类型为IYWP2PPushListener
conversationService.removeP2PPushListener(listener);

群聊消息到达监听

开发者可以通过以下接口添加群聊消息到达监听,为了监听到所有消息,请在登录前添加该监听。具体使用方式可以参考DEMO LoginSampleHelper.addPushMessageListener().

//添加消息到达监听,参数类型为IYWTribePushListener
conversationService.addTribePushListener(listener);
//移除消息到达监听,参数类型为IYWTribePushListener
conversationService.removeTribePushListener(listener);

会话变更监听,包含新增、删除,会话中的数据有变更等

一般集成IMKit的开发者无需添加以下监听,因为SDK内部会进行处理。仅集成IMCore的用户需要添加以下监听,以便及时更新UI。具体使用方式可以参考DEMO ConversationListActivity

//添加会话变更监听,参数类型为IYWConversationListener
conversationService.addConversationListener(listener);
//移除会话变更监听,参数类型为IYWConversationListener
conversationService.removeConversationListener(listener);

所有会话的总未读数变更监听

具体使用方式可以参考DEMO FragmentTabs.initListeners().

//添加未读数变更监听,参数类型为IYWConversationUnreadChangeListener
conversationService.addTotalUnreadChangeListener(listener);
//移除未读数变更监听,参数类型为IYWConversationUnreadChangeListener
conversationService.removeTotalUnreadChangeListener(listener);

发送消息生命周期监听

具体使用方式可以参考DEMO FragmentTabs.initListeners().

//设置发送消息生命周期监听,参数类型为IYWMessageLifeCycleListener
conversationService.setMessageLifeCycleListener(listener);

发送消息给黑名单用户监听

具体使用方式可以参考DEMO FragmentTabs.initListeners().

//设置发送消息给黑名单用户的监听,参数类型为IYWSendMessageToContactInBlackListListener
    conversationService.setSendMessageToContactInBlackListListener(listener);

导入历史会话和消息到本地数据库

导入历史会话到本地数据库

/**
 * 向本地数据库插入历史会话记录
 * @param list      历史会话列表
 * @param callback  回调, 成功回调callback.onSuccess(), 失败回调callback.onError(int code, String info)
 */
public abstract void insertHistoryConversationsToDB(List<IYWConversationModel> list, IWxCallback callback);

//用法
conversationService.insertHistoryConversationsToDB(list, callback);

导入历史消息到本地数据库

/**
 * 向本地数据库插入历史消息
 * @param list      历史消息列表
 * @param callback  回调, 成功回调callback.onSuccess(), 失败回调callback.onError(int code, String info)
 */
public abstract void insertHistoryMessagesToDB(List<IYWMessageModel> list, IWxCallback callback);

//用法
conversationService.insertHistoryMessagesToDB(list, callback);

FAQ

关于此文档暂时还没有FAQ
返回
顶部