Get message history
Retrieve historical messages from the local database, the server, or both.
tip
Cloud message history is different from missed messages. The SDK automatically delivers missed messages (up to 7 days) when connecting — no API call needed.
Get messages with paginated query
Use NCMessagesQuery to retrieve paginated local message history. Create the query object via BaseChannel, then call loadNextPage to fetch each page.
- Swift
- Objective-C
swift
import NexconnChatSDK
// Build query parameters
let identifier = ChannelIdentifier(channelType: .direct, channelId: "targetUserId")
let params = MessagesQueryParams(channelIdentifier: identifier)
params.pageSize = 20
params.startTime = 0 // 0 = start from latest messages
params.isAscending = false // descending order (newest first)
// Create the query object
let query = BaseChannel.createMessagesQuery(params: params)
// Load the first page
query.loadNextPage { page, error in
if let error {
print("Failed: \(error.localizedDescription)")
return
}
// page?.data: current page of messages, newest first
// Call loadNextPage again to get the next older page
}
Objective C
// Build query parameters
NCChannelIdentifier *identifier =
[[NCChannelIdentifier alloc] initWithChannelType:NCChannelTypeDirect
channelId:@"targetUserId"];
NCMessagesQueryParams *params = [[NCMessagesQueryParams alloc] initWithChannelIdentifier:identifier];
params.pageSize = 20;
params.startTime = 0; // 0 = start from latest messages
params.isAscending = NO; // descending order (newest first)
// Create the query object
NCMessagesQuery *query = [NCBaseChannel createMessagesQueryWithParams:params];
// Load the first page
[query loadNextPageWithCompletion:^(NSArray<NCMessage *> *messages, NCError *error) {
if (error) {
NSLog(@"Failed: %@", error.localizedDescription);
return;
}
// messages: current page of messages, newest first
// Call loadNextPage again to get the next older page
}];
tip
Call loadNextPage repeatedly to paginate through history. The query object maintains the pagination anchor internally. When no more messages are available, loadNextPage returns an empty array.
NCMessagesQueryParams properties:
| Property | Type | Default | Description |
|---|---|---|---|
channelIdentifier | NCChannelIdentifier * | — | Channel identifier (required) |
pageSize | NSInteger | 20 | Number of messages per page |
startTime | int64_t | 0 | Pagination anchor timestamp in milliseconds. 0 starts from the latest messages. |
isAscending | BOOL | NO | Sort order. NO for newest-first (descending), YES for oldest-first (ascending). |
Get messages around a specific time
Retrieve messages before and after a given timestamp using getMessagesAroundTime on a channel instance:
- Swift
- Objective-C
swift
import NexconnChatSDK
guard let channel = DirectChannel(channelId: "targetUserId") else { return }
let params = GetMessagesAroundTimeParams(sentTime: timestamp)
params.beforeCount = 10
params.afterCount = 10
channel.getMessagesAroundTime(params: params) { messages, error in
// messages: combined list of messages before and after the timestamp
}
Objective C
NCDirectChannel *channel = [[NCDirectChannel alloc] initWithChannelId:@"targetUserId"];
NCGetMessagesAroundTimeParams *params = [[NCGetMessagesAroundTimeParams alloc] initWithSentTime:timestamp];
params.beforeCount = 10;
params.afterCount = 10;
[channel getMessagesAroundTimeWithParams:params
completion:^(NSArray<NCMessage *> *messages, NCError *error) {
// messages: combined list of messages before and after the timestamp
}];