Browse SDKs · Android
Platform
SDKsAndroidEnterprise

Load message context

Retrieve messages before and after a specified message with the Android SDK.

Copy

Retrieve context messages surrounding a designated anchor message in both earlier and later directions simultaneously. This is commonly used when jumping into a chat from global search results, quoted replies, or push notifications to reconstruct the surrounding conversation thread.

Code example

String conversationID = "si_user_a_user_b";
int before = 20; // Number of earlier messages to load
int after = 20;  // Number of later messages to load

OpenIMClient.getInstance().messageManager.fetchSurroundingMessages(
    new OnBase<Messages>() {
        @Override
        public void onSuccess(Messages messages) {
            List<Message> surrounding = messages.getData();
            // surrounding contains the target anchor message and surrounding context messages
        }

        @Override
        public void onError(int code, String error) {
            // Failed to load surrounding messages
        }
    },
    conversationID,
    targetMessage,
    ViewType.Search,
    before,
    after
);

Parameters

ParameterTypeRequiredDescription
baseOnBase<Messages>YesCallback handler for the asynchronous request.
conversationIDStringYesGlobally unique ID of the conversation containing the anchor message.
startMsgMessageYesAnchor message object used as the center reference point.
typeViewTypeYesContext query view type: pass ViewType.History when jumping from quotes, or ViewType.Search when jumping from search.
beforeintYesMaximum number of messages to retrieve before (earlier than) the anchor.
afterintYesMaximum number of messages to retrieve after (later than) the anchor.

Return value

Upon success, onSuccess returns a list of context messages (List<Message>), including the anchor message itself.

State changes after the call

  1. Snapshot retrieval: This is a read-only query that loads point-in-time message records without altering message delivery states.
  2. UI positioning: Populate the chat timeline and scroll to the anchor message, using clientMsgID for deduplication and focus highlighting.