Skip to main content

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
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
}
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:

PropertyTypeDefaultDescription
channelIdentifierNCChannelIdentifier *Channel identifier (required)
pageSizeNSInteger20Number of messages per page
startTimeint64_t0Pagination anchor timestamp in milliseconds. 0 starts from the latest messages.
isAscendingBOOLNOSort 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
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
}