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从会话列表获取会话,若会话列表中不存在该会话,则该方法会返回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);