Chat / iOS
Chat iOS v4
Chat iOS
Chat
iOS
Version 4
Home
/
Chat
/
iOS
/
Message

Load messages by timestamp or message ID

Copy link

Using the getMessagesByTimestamp(:params:completionHandler:) method or the getMessagesByMessageId(_:params:completionHandler:) method, you can retrieve a set number of previous and next messages by specifying a timestamp or message ID in a channel.

Note: The getPreviousMessages() method is deprecated as of August 2021. Accordingly, use the getMessagesByTimestamp(:params:completionHandler:) method instead.

The following code shows several types of parameters that you can configure to customize a message query by using MessageListParams. Under the MessageListParams object, you can assign specific values to previousResultSize, messageTypeFilter, and other properties.

let params = MessageListParams()
params.isInclusive = IS_INCLUSIVE
params.previousResultSize = PREVIOUS_RESULT_SIZE
params.nextResultSize = NEXT_RESULT_SIZE
params.reverse = REVERSE
params.messageTypeFilter = MESSAGE_TYPE
params.customType = CUSTOM_TYPE

MessageListParams

Copy link

This table only contains properties shown in the code above. See the API reference for a complete list of properties.

Property nameTypeDescription

isInclusive

Bool

Determines whether to include messages sent exactly on the specified timestamp or have the matching message ID.

previousResultSize

Int

Specifies the number of messages to retrieve, which are sent previously before a specified timestamp. Note that the actual number of results may be larger than the set value when there are multiple messages with the same timestamp as the earliest message.

nextResultSize

Int

Specifies the number of messages to retrieve, which are sent later after a specified timestamp. Note that the actual number of results may be larger than the set value when there are multiple messages with the same timestamp as the latest message.

reverse

Bool

Determines whether to sort the retrieved messages in reverse order. If set to true, messages are returned from the most recent to the oldest. (Default: false)

messageTypeFilter

MessageTypeFilter

Specifies the message type to filter the messages with the corresponding type. Acceptable values are .all, .user, .file, and .admin.

customType

String

Specifies the custom message type to filter the messages with the corresponding custom type.


By timestamp

Copy link

To retrieve messages in a channel using a timestamp, pass the MessageListParams object as an argument to the parameter in the getMessagesByTimestamp(:params:completionHandler:) method.

channel.getMessagesByTimestamp(TIMESTAMP, params: params) { messages, error in
    guard error == nil else {
        // Handle error.
        return
    }

    // A list of previous and next messages of a specified timestamp
    // is successfully retrieved.
    // Through the messages parameter of the callback method,
    // you can access and display the data of each message from the result list
    // that the Sendbird server has passed to the callback method.
}

List of parameters

Copy link
Parameter nameTypeDescription

timestamp

Int64

Specifies the timestamp to be the reference point of a retrieval in Unix milliseconds.

params

MessageListParams

Contains a set of parameters you can use when retrieving messages.


By message ID

Copy link

To retrieve a set number of previous and next messages of a specific message ID in a channel, use the getMessagesByMessageId(_:params:completionHandler:) method and the MessageListParams object.

channel.getMessagesByMessageId(MESSAGE_ID, params: params) { messages, error in
    guard error == nil else {
        // Handle error.
        return
    }

    // A list of previous and next messages of a specified message ID
    // is successfully retrieved.
    // Through the messages parameter of the callback method,
    // you can access and display the data of each message from the result list
    // that the Sendbird server has passed to the callback method.
}

List of parameters

Copy link
Parameter nameTypeDescription

messageId

Int64

Specifies the unique ID of the message to be the reference point of a retrieval.

params

MessageListParams

Contains a set of parameters you can use when retrieving messages.