Browse SDKs · Android
Platform
SDKsAndroid

Send a message

Use sendMessage to send a Message to a one-to-one or group conversation.

Copy

Transmit a previously constructed Message object to a single chat (C2C) or group chat. For single chat, specify the recipient user ID (recvUid) and pass null for recvGid. For group chat, specify the target group ID (recvGid) and pass null for recvUid. These two parameters are mutually exclusive. The OnMsgSendCallback provides granular callbacks for upload progress, success, and delivery failure.

Code example

String peerUserID = "user_b";
OfflinePushInfo offlinePushInfo = new OfflinePushInfo();
offlinePushInfo.setTitle("New Message Notification");
offlinePushInfo.setDesc("You received a new message. Tap to view.");

OpenIMClient.getInstance().messageManager.sendMessage(
    new OnMsgSendCallback() {
        @Override
        public void onProgress(long progress) {
            // progress indicates media upload percentage (0-100)
        }

        @Override
        public void onSuccess(Message sentMessage) {
            // Message sent successfully, update local state using sentMessage.getClientMsgID()
        }

        @Override
        public void onError(int code, String error) {
            // Mark message delivery failed in UI and provide retry option
        }
    },
    message,
    peerUserID,
    null,
    offlinePushInfo
);

Parameters

ParameterTypeRequiredDescription
callbackOnMsgSendCallbackYesLifecycle callback for message transmission (progress, success, error).
messageMessageYesThe message object to transmit, created by the corresponding factory method.
recvUidStringConditionalRecipient user ID for single chat. Must be null when sending to a group.
recvGidStringConditionalTarget group ID for group chat. Must be null when sending to a single user.
offlinePushInfoOfflinePushInfoNoCustom offline push notification payload. Pass null for default push configuration.

Return value

Upon success, onSuccess returns the transmitted Message object. Key populated attributes include serverMsgID, timestamp sendTime, and updated status status.

State changes after the call

  1. Callback confirmation: The onSuccess callback indicates that the server accepted and stored the message. The client should update the message status in the UI from "sending" to "sent" using clientMsgID.
  2. Asynchronous event delivery: Delivery to recipient devices occurs asynchronously. The receiving client receives the message through OnAdvanceMsgListener.onRecvNewMessage.
  3. Re-query calibration: If delivery status is uncertain due to network timeout or client disconnection, query message history via Load older messages to verify whether the message was persisted.