Send your first message
Send and verify your first direct text message in a native Android application.
Sending a single direct text message verifies that SDK initialization, connection establishment, and message delivery pipelines are functioning properly. Before proceeding, complete SDK initialization and user login as described in Authenticate and manage a session.
Prerequisites
- Prepare two test user accounts: one for the sender and one for the recipient.
- Initialize the SDK on both accounts following Authenticate and manage a session; the recipient must register the application message listener prior to logging in.
- Replace
user_bin the examples below with the actual recipient'suserID.
Step 1: Listen for incoming messages on the recipient client
The recipient client registers the message listener before logging in and updates the chat interface when onRecvNewMessage(Message message) fires. For complete listener registration details, event payloads, and dispatch architecture, see Receive messages. Do not duplicate message listener registrations inside individual chat screens.
Step 2: Confirm connection readiness
Once both users log in and the sender's client receives onConnectSuccess from OnConnListener, the client is ready to send messages. Ensure the recipient maintains an active connection to receive messages immediately. If disconnected, wait for reconnection rather than repeating login calls.
Step 3: Send a text message
Send a direct chat text message
Construct the message entity first, then dispatch it. Pass the recipient userID as the target, leaving the group ID parameter as an empty string.
Message message = OpenIMClient.getInstance()
.messageManager
.createTextMessage("Hello, OpenIM");
OnMsgSendCallback sendCallback = new OnMsgSendCallback() {
@Override
public void onProgress(long progress) {
// Text messages typically do not report upload progress.
}
@Override
public void onError(int code, String error) {
// Notify the user of failure and enable retry.
}
@Override
public void onSuccess(Message sentMessage) {
// Update the sender UI with the delivered sentMessage.
}
};
OpenIMClient.getInstance().messageManager.sendMessage(
sendCallback,
message,
"user_b",
"",
new OfflinePushInfo()
);OfflinePushInfo configures push notification behavior. Pass a default empty instance for initial verification, then populate title, summary, and custom payload fields as required.
When onSuccess triggers on the sender client, the message has been delivered to the server. When onRecvNewMessage fires on the recipient client, the message has arrived on the remote device. When both callbacks fire with matching content, your end-to-end messaging pipeline is verified.
Send a group chat text message
To send a group message, set recvUid to an empty string and pass the destination groupID:
Message groupMessage = OpenIMClient.getInstance()
.messageManager
.createTextMessage("Hello everyone, OpenIM");
OpenIMClient.getInstance().messageManager.sendMessage(
sendCallback,
groupMessage,
"",
"group_123",
new OfflinePushInfo()
);Supply either recvUid or groupID in a single invocation, leaving the other as an empty string.
Verify delivery
- The sender receives a successful
onSuccesscallback viaOnMsgSendCallback. - The recipient receives
onRecvNewMessageviaOnAdvancedMsgListenerand renders the text message content.
Next steps
Was this page helpful?