- Android
- iOS
- JavaScript
- 鸿蒙
接口定义
/**
* 获取消息,结果按照消息时间正序排列(旧的在前,新的在后)。当消息有缺失并且网络有问题的时候,返回本地缓存的消息。
* @param conversation 会话对象
* @param direction 拉取方向
* @param options 获取消息选项
* @param callback 回调
*/
void getMessages(Conversation conversation,
JIMConst.PullDirection direction,
GetMessageOptions options,
IGetMessagesCallbackV3 callback);
interface IGetMessagesCallbackV3 {
/**
* 结果回调
* @param messages 消息列表
* @param timestamp 消息时间戳,拉下一批消息的时候可以使用
* @param hasMore 是否还有更多消息
* @param code 结果码,0 为成功。code 不为 0 的时候,如果本地存在缓存消息,则会在 messages 里返回本地消息
*/
void onGetMessages(List<Message> messages, long timestamp, boolean hasMore, int code);
}
示例代码
GetMessageOptions options = new GetMessageOptions();
options.setCount(100);
options.setStartTime(0);
Conversation conversation = new Conversation(Conversation.ConversationType.PRIVATE, "userid2");
JIM.getInstance().getMessageManager().getMessages(conversation, JIMConst.PullDirection.OLDER, options, new IMessageManager.IGetMessagesCallbackV3() {
@Override
public void onGetMessages(List<Message> messages, long timestamp, boolean hasMore, int code) {
Log.d("TAG", "messageList count is " + messages.size());
}
});
接口定义
/// 获取消息,结果按照消息时间正序排列(旧的在前,新的在后)。当消息有缺失并且网络有问题的时候,返回本地缓存的消息。
/// - Parameters:
/// - conversation: 会话对象
/// - direction: 拉取方向
/// - option: 获取消息选项
/// - completeBlock: messages: 消息列表,timestamp: 消息时间戳,拉下一批消息的时候可以使用,hasMore: 是否还有更多消息,
/// code: 错误码(code 不为 0 的时候,如果本地存在缓存消息,则会在 messages 里返回本地消息)
- (void)getMessages:(JConversation *)conversation
direction:(JPullDirection)direction
option:(JGetMessageOptions *)option
complete:(void (^)(NSArray <JMessage *> *messages, long long timestamp, BOOL hasMore, JErrorCode code))completeBlock;
示例代码
JGetMessageOptions *options = [[JGetMessageOptions alloc] init];
options.count = 100;
options.startTime = 0;
JConversation *conversation = [[JConversation alloc] initWithConversationType:JConversationTypePrivate conversationId:@"userid2"];
[JIM.shared.messageManager getMessages:conversation
direction:JPullDirectionOlder
option:options
complete:^(NSArray<JMessage *> *messages, long long timestamp, BOOL hasMore, JErrorCode code) {
NSLog(@"getMessages count is %ld", messages.count);
}];
参数说明
名称 | 类型 | 必填 | 默认值 | 描述 | 版本 |
---|---|---|---|---|---|
params | Object | 是 | 历史消息获取参数 | 1.0.0 | |
params.conversationType | Number | 是 | 会话类型 | 1.0.0 | |
params.conversationId | String | 是 | 会话 Id,会话类型是 PRIVATE 时,会话 Id 是对方的 userId,会话类型是 GROUP 时是群组 Id | 1.0.0 | |
params.count | Object | 否 | 20 | 历史消息获取条数,获取历史消息条数范围 1 - 20 条 | 1.0.0 |
params.time | Number | 否 | 0 | 从指定时间开始获取历史消息,可用于调到历史某一条消息,获取前后消息 | 1.0.0 |
params.order | Number | 否 | BACKWARD | 获取历史消息方向,BACKWARD 获取更早的历史消息 | 1.0.0 |
成功回调
名称 | 类型 | 描述 | 版本 |
---|---|---|---|
result | Object | 1.0.0 | |
result.isFinished | Object | 是否还有更多的历史消息没有获取 | 1.0.0 |
result.messages | Object | 消息数组,每条消息的属性,请查看 Message 结构 | 1.0.0 |
失败回调
名称 | 类型 | 描述 | 版本 |
---|---|---|---|
error | Object | 发送失败后会有对应的状态码,可以直接查看 error.msg ,或者查看 状态码 | 1.0.0 |
示例代码
let { ConversationType } = JIM;
let params = {
conversationType: ConversationType.PRIVATE,
conversationId: 'userid2'
};
jim.getMessages(params).then((result) => {
let { messages, isFinished } = result;
console.log(messages, isFinished);
}, (error) => {
console.log(error);
})
接口定义
/**
* 结果回调
* @param code 响应错误码,0为成功
* @param msgs 消息列表
* @param hasMore 是否还有更多消息
*/
export type QryMessagesCallback = (code:number,msgs:Message[],hasMore:boolean)=>void
/**
* 获取消息,结果按照消息时间正序排列(旧的在前,新的在后)。当消息有缺失并且网络有问题的时候,返回本地缓存的消息。
* @param conver 会话对象
* @param options 消息拉取选项
* @param callback 回调
*/
queryMessages(conver:Conversation,options:QueryMsgOptions,callback:QryMessagesCallback)
示例代码
let options = new QueryMsgOptions()
options.count = 100
options.startTime = 0
options.isPositive = false
JuggleIm.instance.getMessageManager().queryMessages(new Conversation("userid1",1),options,(code,msgs,hasMore)=>{
})