获取会话
IMLib SDK 会根据用户收发的消息,在本地数据库中生成对应会话,并维护会话列表。应用程序可以获取本地数据库中的会话列表。自 5.20.0 版本开始,SDK 支持在获取会话列表中返回超级群会话信息,以便于开发者根据业务需求对单聊、群聊、超级群会话列表进行混合排序展示;该功能需提交工单开启。
获取指定单个会话
获取某个会话的详细信息。
接口
RongCoreClient.getInstance().getConversation(conversationType, targetId, callback)
参数说明
参数 | 类型 | 说明 |
---|---|---|
conversationType | ConversationType | 会话类型 |
targetId | String | 会话 ID |
callback | ResultCallback<Conversation> | 回调接口 |
示例代码
String conversationType = ConversationType.PRIVATE;
String targetId = "会话 Id";
RongCoreClient.getInstance().getConversation(conversationType, targetId, new IRongCoreCallback.ResultCallback<Conversation>() {
@Override
public void onSuccess(Conversation conversation) {
// 成功并返回会话信息
}
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
}
});
批量获取会话信息
除了单个获取会话信息外,IMLib SDK 还支持批量获取会话的详细信息。
接口
RongCoreClient.getInstance().getConversations(conversationIdentifiers, callback)
参数说明
参数 | 类型 | 说明 |
---|---|---|
conversationIdentifiers | List<ConversationIdentifier> | 会话类型 |
callback | ResultCallback<List<Conversation>> | 回调接口 |
注意: IMLib SDK 从 5.8.2 版本开始支持批量获取会话信息。 支持的会话类型:单聊、群聊、系统。
使用 getConversations()
方法查询多个会话的详细信息。
示例代码
// 假设我们有会话标识 conIden1 和 conIden2,它们代表想要查询的会话。
List<ConversationIdentifier> conversationIdentifiers = new ArrayList<>();
conversationIdentifiers.add(ConversationIdentifier.obtain(ConversationType.PRIVATE, "tId1", ""));
conversationIdentifiers.add(ConversationIdentifier.obtain(ConversationType.PRIVATE, "tId2", ""));
RongCoreClient.getInstance().getConversations(conversationIdentifiers, new IRongCoreCallback.ResultCallback<List<Conversation>>() {
@Override
public void onSuccess(List<Conversation> conversations) {
// 成功并返回会话信息
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode e) {
}
});
获取会话列表
会话列表是 SDK 在本地生成和维护的。如果未发生卸载重载或换设备登录,您可以获取本地设备上存储的所有历史消息生成的会话列表。
分页获取会话列表
分页获取 SDK 在本地数据库生成的会话列表。返回的会话列表按照时间倒序排列。如果返回结果含有被设置为置顶状态的会话,则置顶会话默认排在最前。
使用 getConversationListByPage 方法,时间戳(startTime
)首次可传 0
,后续可以使用返回的 Conversation 对象的 sentTime
或 operationTime
属性值为下一次查询的 startTime
。注:如果您使用的是 5.6.8 之后的版本,请使用 operationTime
字段,不然可能会有数据拉取遗漏的问题。
本接口从 5.20.0 版本支持获取超级群默认频道会话,如需要在列表中获取超级群,请升级到 IM 尊享版后,提交工单申请开通。
接口
RongCoreClient.getInstance().getConversationListByPage(callback,timeStamp, count, topPriority, conversationTypes);
参数说明
参数 | 类型 | 说明 |
---|---|---|
callback | ResultCallback<List<Conversation>> | 方法回调。 |
timeStamp | long | 时间戳,以获取早于这个时间戳的会话列表。首次可传 0,表示从最新开始获取。后续使用真实时间戳。 |
count | int | 取回的会话数量。建议此数值不要超过 10 个,当一次性获取的会话数过大时,会导致跨进程通信崩溃,引发获取会话列表失败及通信连接被中断。 当实际取回的会话数量小于 count 值时,表明已取完数据。 |
topPriority | boolean | 是否优先显示置顶消息。要求 SDK 版本 ≧ 5.6.9。 |
conversationTypes | Array of ConversationType | 选择要获取的会话类型。可设置多个会话类型。 |
示例代码
long timeStamp = 0;
int count = 10;
Conversation.ConversationType[] conversationTypes = {ConversationType.PRIVATE, ConversationType.GROUP};
RongCoreClient.getInstance().getConversationListByPage(new IRongCoreCallback.
ResultCallback<List<Conversation>>() {
@Override
public void onSuccess(List<Conversation> conversations) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode ErrorCode) {
}
},timeStamp, count, conversationTypes);
如果希望返回的会话列表严格按照时间倒序排列,请使用带 topPriority
参数的重载方法,并将该参数设置为 false
。该重载方法仅在 5.6.9 及之后版本提供。
long timeStamp = 0;
int count = 10;
boolean topPriority = false;
Conversation.ConversationType[] conversationTypes = {ConversationType.PRIVATE, ConversationType.GROUP};
RongCoreClient.getInstance().getConversationListByPage(new IRongCoreCallback.
ResultCallback<List<Conversation>>() {
@Override
public void onSuccess(List<Conversation> conversations) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode ErrorCode) {
}
},timeStamp, count, topPriority, conversationTypes);