Send messages
The built-in channel pages in ChatUI already implement message sending. To send messages from custom pages, use the NCChatUI send methods to ensure internal page state synchronization.
Important
- Send regular messages:
NCChatUI.sendMessage(ChannelIdentifier, SendMessageParams, SendMessageHandler)- Send media messages:
NCChatUI.sendMediaMessage(ChannelIdentifier, SendMediaMessageParams, SendMediaMessageHandler)
Send regular messages
Method
Java
NCChatUI.sendMessage(identifier, params, handler);
Parameters
| Parameter | Type | Description |
|---|---|---|
identifier | ChannelIdentifier | Target channel identifier (channel type + channel ID) |
params | SendMessageParams | Regular message send parameters. content required. |
handler | SendMessageHandler | Callback handler. Can be null. |
Example
Java
String targetId = "Channel ID";
ChannelIdentifier identifier = new ChannelIdentifier(ChannelType.DIRECT, targetId);
TextMessage content = new TextMessage("Message content");
SendMessageParams params = new SendMessageParams(content);
// Optional: push configuration
PushConfig pushConfig = new PushConfig();
pushConfig.setPushContent("You have a new message");
pushConfig.setPushData("{\"biz\":\"chat\"}");
params.setPushConfig(pushConfig);
NCChatUI.sendMessage(identifier, params, new SendMessageHandler() {
@Override
public void onAttached(Message message) {
// Message written to local database
}
@Override
public void onResult(Message message, NCError error) {
if (error == null) {
// Send succeeded
} else {
// Send failed
}
}
});
Send media messages
Method
Java
NCChatUI.sendMediaMessage(identifier, params, handler);
Parameters
| Parameter | Type | Description |
|---|---|---|
identifier | ChannelIdentifier | Target channel identifier (channel type + channel ID) |
params | SendMediaMessageParams | Media message send parameters. content required. |
handler | SendMediaMessageHandler | Callback handler. Can be null. |
Example (image)
Java
String targetId = "Channel ID";
ChannelIdentifier identifier = new ChannelIdentifier(ChannelType.DIRECT, targetId);
ImageMessage image = new ImageMessage();
image.setLocalPath("file:///sdcard/Download/demo.jpg");
image.setOriginal(true); // Optional: original image
SendMediaMessageParams params = new SendMediaMessageParams(image);
NCChatUI.sendMediaMessage(identifier, params, new SendMediaMessageHandler() {
@Override
public void onAttached(Message message) {
// Message saved to database
}
@Override
public void onProgress(Message message, int progress) {
// Upload progress
}
@Override
public void onCanceled(Message message) {
// Send canceled
}
@Override
public void onResult(Message message, NCError error) {
if (error == null) {
// Send succeeded
} else {
// Send failed
}
}
});
Directional messages (group/community)
ChatUI does not currently provide separate sendDirectionalMessage(...) or sendDirectionalMediaMessage(...) methods.
To send directional messages, set directedUserIds in the parameter object:
Java
ChannelIdentifier identifier = new ChannelIdentifier(ChannelType.GROUP, "Group ID");
TextMessage content = new TextMessage("Visible only to specified members");
SendMessageParams params = new SendMessageParams(content);
params.setDirectedUserIds(new String[] {"u1", "u2"});
NCChatUI.sendMessage(identifier, params, handler);
For media messages, use SendMediaMessageParams#setDirectedUserIds(...).
Push and additional configuration
SendMessageParams and SendMediaMessageParams support the following common configuration options:
| Field | Type | Description |
|---|---|---|
pushConfig | PushConfig | Custom push title, content, additional data, etc. |
disableNotification | boolean | Whether to disable push notification for this message |
disableUpdateLastMessage | boolean | Whether to skip updating the channel's last message |
metadata | Map<String, String> | Custom metadata |
needReceipt | boolean | Whether to request read receipt |
Example:
Java
SendMessageParams params = new SendMessageParams(new TextMessage("hello"));
params.setDisableNotification(true);
params.setNeedReceipt(true);
Notes
- All examples in this document use the current
NCChatUIpublic API. - When constructing messages, use the message content classes and parameter objects provided by the current SDK.
- Callbacks use
onResult(...)to return success or failure.