Browse SDKs · Android
Platform
SDKsAndroid

Load older messages

Use getAdvancedHistoryMessageList to load conversation history by page.

Copy

Retrieve historical messages backwards in time (towards older messages) within a specified conversation. For the initial page load, pass null for startMsg. For subsequent pages, pass the oldest message from the previous batch as the new startMsg.

Code example

String conversationID = "si_user_a_user_b";
Message startMessage = null; // Pass null for the initial page, or the oldest message from the last batch
int count = 20;

OpenIMClient.getInstance().messageManager.getAdvancedHistoryMessageList(
    new OnBase<AdvancedMessage>() {
        @Override
        public void onSuccess(AdvancedMessage result) {
            List<Message> messages = result.getMessageList();
            boolean reachedBeginning = result.isEnd();
            // Prepend deduplicated messages to the top of the chat timeline using clientMsgID
        }

        @Override
        public void onError(int code, String error) {
            // Failed to load historical messages
        }
    },
    conversationID,
    startMessage,
    count,
    ViewType.History
);

Parameters

ParameterTypeRequiredDescription
baseOnBase<AdvancedMessage>YesCallback handler for the asynchronous request.
conversationIDStringYesGlobally unique ID of the target conversation.
startMsgMessageNoAnchor message for pagination. Pass null for initial load, or the oldest message from the preceding batch.
countintYesMaximum number of messages to retrieve in this batch.
viewTypeViewTypeYesView mode enum. Pass ViewType.History for regular history loads.

Return value

Upon success, onSuccess returns an AdvancedMessage object:

FieldDescription
messageListList of retrieved messages (List<Message>) for the current page.
isEndIndicates whether the beginning of history has been reached (true means no earlier messages exist, false means more can be loaded).
errCodeResult error code.
errMsgResult error description.

State changes after the call

  1. Snapshot retrieval: This is a read-only query that loads point-in-time message records without emitting new-message events.
  2. UI integration: Prepend messages to the chat view, deduplicating records using clientMsgID.