SDKsAndroidEnterprise
Load message context
Retrieve messages before and after a specified message with the Android SDK.
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
| Parameter | Type | Required | Description |
|---|---|---|---|
base | OnBase<Messages> | Yes | Callback handler for the asynchronous request. |
conversationID | String | Yes | Globally unique ID of the conversation containing the anchor message. |
startMsg | Message | Yes | Anchor message object used as the center reference point. |
type | ViewType | Yes | Context query view type: pass ViewType.History when jumping from quotes, or ViewType.Search when jumping from search. |
before | int | Yes | Maximum number of messages to retrieve before (earlier than) the anchor. |
after | int | Yes | Maximum 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
- Snapshot retrieval: This is a read-only query that loads point-in-time message records without altering message delivery states.
- UI positioning: Populate the chat timeline and scroll to the anchor message, using
clientMsgIDfor deduplication and focus highlighting.
Was this page helpful?