SDKsAndroid
Send a message
Use sendMessage to send a Message to a one-to-one or group conversation.
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
| Parameter | Type | Required | Description |
|---|---|---|---|
callback | OnMsgSendCallback | Yes | Lifecycle callback for message transmission (progress, success, error). |
message | Message | Yes | The message object to transmit, created by the corresponding factory method. |
recvUid | String | Conditional | Recipient user ID for single chat. Must be null when sending to a group. |
recvGid | String | Conditional | Target group ID for group chat. Must be null when sending to a single user. |
offlinePushInfo | OfflinePushInfo | No | Custom 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
- Callback confirmation: The
onSuccesscallback indicates that the server accepted and stored the message. The client should update the message status in the UI from "sending" to "sent" usingclientMsgID. - Asynchronous event delivery: Delivery to recipient devices occurs asynchronously. The receiving client receives the message through
OnAdvanceMsgListener.onRecvNewMessage. - 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.
Was this page helpful?