Chat / Android
Chat Android v4
Chat Android
Chat
Android
Home
/
Chat
/
Android
/
Message
This is the new Docs for Chat SDK v4 for Android. To see the previous Docs, click here.

Load previous messages

Using PreviousMessageListQuery's load() method, which returns a list of BaseMessage objects, you can retrieve a set number of previous messages in a channel. With a returned list, you can display the past messages in your UI once they have loaded. The following code is an example of retrieving previous messages in a channel.

// There should only be a single instance per channel view.
val query = channel.createPreviousMessageListQuery(
    PreviousMessageListQueryParams().apply {
        messagePayloadFilter = MessagePayloadFilter().apply {
            includeMetaArray = true // Retrieve a list of messages along with their meta arrays.
            includeReactions = true // Retrieve a list of messages along with their reactions.
        }
        limit = LIMIT
        reverse = REVERSE
    }
)

// Retrieve previous messages.
query.load() { messages, e ->
    if (e != null) {
        // Handle error.
    }

    // ...
}

PreviousMessageListQueryParams

Property nameTypeDescription

limit

int

Specifies the number of results to return per call. Acceptable values are 1 to 100, inclusive. The recommended value for this parameter is 20.

reverse

boolean

Determines whether to sort the retrieved messages in reverse order. If true, returns a list of messages which the latest comes at first and the earliest at last. the results are sorted in reverse order. If false, returns a list of messages which the earliest comes at first and the latest at last.

The limit property indicates how many messages should be included in a returned list. The PreviousMessageListQuery instance itself does the pagination of a result set according to the value of the limit property and internally manages a token to retrieve the next page in the result set.

Each time the load() method is called, the instance retrieves a set number of messages in the next page and updates the value of the token to complete the current call and prepare the next call. Before calling the load() method again, you must receive a success callback through the callback handler first.

If you create a new PreviousMessageListQuery instance and call the load() method, a set number of the most recently sent messages are retrieved because the new instance's token has nothing to do with the previously created instance. So we recommend that you create a single query instance and store it as a member variable for traversing through the entire message history.

The following table shows all the supported filters for PreviousMessageListQuery to search for messages you want to retrieve. You can use any filters in a similar fashion with the sample code above.

List of filters

NameFilters

messageTypeFilter

Messages with the specified message type. The messageTypeFilter property can be used to enable this filter.

customTypesFilter

Messages with the specified custom type. The customTypesFilter property can be used to enable this filter.

senderUserIdsFilter

Messages that are sent by the specified users. The senderUserIdsFilter property can be used to enable this filter.

messagePayloadFilter

Messages including additional information such as meta arrays and reactions.