Skip to content
能力中心
产品中心
应用市场
WebOffice
开发者后台

获取历史消息

可从本地或远端获取历史消息, 协作中台历史消息会在云端完全保存。

获取指定时间之前的消息

本地远端结合获取

接口定义

swift
func loadMessages(chatId: String, before time: Int64, limit: Int, completion: @escaping((_ msgs: [KIMMessage]?, _ hasNext: Bool, _ error: KIMError?) -> Void))
Objective-C
- (void)loadMessagesWithChatId:(NSString *)chatId before:(int64_t)time limit:(NSInteger)limit completion:(void (^)(NSArray<KIMMessage *> *, BOOL, KIMError *))completion;

参数说明

属性类型说明
chatIdString会话 id
timeInt64ns,查询开始时间点(不包含 time),若要从最新消息开始则传 0
limitInt查询的消息数量
complete(_ msgs: [KIMMessage], _ hasNext: Bool, _ error: KIMError?) -> Void)?完成回调。 msgs:返回的消息列表; hasNext:是否还有更多消息; error:出现错误时的错误信息

代码示例

swift
KIMCore.shared.loadMessages(chatId: chatId, before: time, limit: limit) { msgs, hasNext, error in

}

仅从远端获取

接口定义

swift
func loadRemoteMessages(chatId: String, before time: Int64, limit: Int, completion: @escaping((_ msgs: [KIMMessage]?, _ hasNext: Bool, _ error: KIMError?) -> Void))
Objective-C
- (void)loadRemoteMessagesWithChatId:(NSString *)chatId before:(int64_t)time limit:(NSInteger)limit completion:(void (^)(NSArray<KIMMessage *> *, BOOL, KIMError *))completion;

参数说明

属性类型说明
chatIdString会话 id
timeInt64ns,查询开始时间点(不包含),若要从最新消息开始则传 0
limitInt查询的消息数量
complete(_ msgs: [KIMMessage], _ hasNext: Bool, _ error: KIMError?) -> Void)?完成回调。 msgs:返回的消息列表; hasNext:是否还有更多消息; error:出现错误时的错误信息

代码示例

swift
KIMCore.shared.loadRemoteMessages(chatId: chatId, before: time, limit: limit) { msgs, hasNext, error in

}

仅从本地获取

接口定义

swift
func loadLocalMessages(chatId: String, before time: Int64, limit: Int) -> [KIMMessage]
Objective-C
- (NSArray<KIMMessage *> *)loadLocalMessagesWithChatId:(NSString *)chatId before:(int64_t)time limit:(NSInteger)limit;

参数说明

属性类型说明
chatIdString会话 id
timeInt64ns,查询开始时间点(不包含),若要从最新消息开始则传 0
limitInt查询的消息数量

代码示例

swift
let msgs = KIMCore.shared.loadLocalMessages(chatId: chatId, before: time, limit: limit)

获取指定时间之后的消息

本地远端结合获取

接口定义

swift
func loadMessages(chatId: String, after time: Int64, limit: Int, completion: @escaping((_ msgs: [KIMMessage]?, _ hasNext: Bool, _ error: KIMError?) -> Void))
Objective-C
- (void)loadMessagesWithChatId:(NSString *)chatId after:(int64_t)time limit:(NSInteger)limit completion:(void (^)(NSArray<KIMMessage *> *, BOOL, KIMError *))completion;

参数说明

属性类型说明
chatIdString会话 id
timeInt64ns,查询开始时间点(不包含),若要从最新消息开始则传 0
limitInt查询的消息数量
complete(_ msgs: [KIMMessage], _ hasNext: Bool, _ error: KIMError?) -> Void)?完成回调。 msgs:返回的消息列表; hasNext:是否还有更多消息; error:出现错误时的错误信息

代码示例

swift
KIMCore.shared.loadMessages(chatId: chatId, after: time, limit: limit) { msgs, hasNext, error in

}

仅从远端获取

接口定义

swift
func loadRemoteMessages(chatId: String, after time: Int64, limit: Int, completion: @escaping((_ msgs: [KIMMessage]?, _ hasNext: Bool, _ error: KIMError?) -> Void))
Objective-C
- (void)loadRemoteMessagesWithChatId:(NSString *)chatId after:(int64_t)time limit:(NSInteger)limit completion:(void (^)(NSArray<KIMMessage *> *, BOOL, KIMError *))completion;

参数说明

属性类型说明
chatIdString会话 id
timeInt64ns,查询开始时间点(不包含),若要从最新消息开始则传 0
limitInt查询的消息数量
complete(_ msgs: [KIMMessage], _ hasNext: Bool, _ error: KIMError?) -> Void)?完成回调。 msgs:返回的消息列表; hasNext:是否还有更多消息; error:出现错误时的错误信息

代码示例

swift
KIMCore.shared.loadRemoteMessages(chatId: chatId, after: time, limit: limit) { msgs, hasNext, error in

}

仅从本地端获取

接口定义

swift
func loadLocalMessages(chatId: String, after time: Int64, limit: Int) -> [KIMMessage]
Objective-C
- (NSArray<KIMMessage *> *)loadLocalMessagesWithChatId:(NSString *)chatId after:(int64_t)time limit:(NSInteger)limit;

参数说明

属性类型说明
chatIdString会话 id
timeInt64ns,查询开始时间点(不包含),若要从最新消息开始则传 0
limitInt查询的消息数量

代码示例

swift
let msgs = KIMCore.shared.loadLocalMessages(chatId: chatId, after: time, limit: limit)

获取指定消息

指定同个会话中的多个消息 id,同时返回对应的消息数据。

接口定义

swift
func getMessages(chatId: String, msgIds: [String], complete: ((_ msgs: [KIMMessage]?, _ error: KIMError?) -> Void)?)
Objective-C
- (void)getMessagesWithChatId:(NSString *)chatId msgIds:(NSArray<NSString *> *)msgIds complete:(void (^)(NSArray<KIMMessage *> *, KIMError *))complete;

参数说明

属性类型说明
chatIdString会话 id
msgIds[String]需要查询的消息 id 列表
complete(_ msgs: [KIMMessage]?, _ error: KIMError?) -> Void)?获取结果回调。 msgs:返回的消息列表 error:出现错误时的错误信息

代码示例

swift
KIMCore.shared.getMessages(chatId: chatId, msgIds: msgIds) { msgs, error in

}