Browse SDKs · Android
Platform
SDKsAndroid

Insert a local direct message

Use insertSingleMessageToLocalStorage to insert a one-to-one message visible only on the current device.

Copy

Write a pre-constructed message directly into the local SQLite database of the current device for a one-to-one conversation. This operation does not issue a network request to the server, does not deliver the message to the peer user, and does not synchronize to other devices logged into the same account. It is commonly used for inserting local security prompts, client-side assistant messages, or migrating offline records. To send an actual message across the network, call Send a message.

Code example

String receiverUserID = "user_b";
String senderUserID = "user_a";

OpenIMClient.getInstance().messageManager.insertSingleMessageToLocalStorage(
    new OnBase<String>() {
        @Override
        public void onSuccess(String data) {
            // Message written to local storage, refresh direct chat UI
        }

        @Override
        public void onError(int code, String error) {
            // Handle insertion failure
        }
    },
    message,
    receiverUserID,
    senderUserID
);

Parameters

ParameterTypeRequiredDescription
baseOnBase<String>YesCallback handler for the asynchronous request.
messageMessageYesPre-constructed message object (typically created via one of the create*Message APIs).
receiverStringYesUser ID of the direct chat recipient.
senderStringYesUser ID of the direct chat sender. These two fields determine message direction and must not be inverted.

Return value

Upon success, data in onSuccess returns a confirmation message.

State changes after the call

  1. Callback confirmation: The onSuccess callback confirms that the message was saved to local storage on this device. Insert the item into your conversation timeline using clientMsgID as the distinct key.
  2. Local scope: This is a local-only operation that does not trigger network requests or dispatch OnAdvanceMsgListener.onRecvNewMessage events.
  3. Re-query calibration: The inserted message exists only in the local database on this device. It will not synchronize to cloud roaming history or appear on other devices logged into the same account.