Sending Messages
IMKit's built-in chat UI already supports sending various message types (some require plugin integration). When customizing your chat interface, you can use the core IMCenter
class methods to send messages. These methods not only handle message delivery but also trigger updates in IMKit's built-in interfaces.
IMKit supports sending both standard messages and media messages (see [Message Introduction]). The parent class for standard messages is [MessageContent], while media messages inherit from [MediaMessageContent]. The key difference lies in whether data upload is required for media messages.
Important
- Always use
IMCenter
methods for sending messages to ensure UI updates. UsesendMessage
for standard messages andsendMediaMessage
for media messages.- The client SDK enforces a rate limit of 5 messages per second.
Sending Standard Messages
Construct a Message
object before sending. The content
field must contain a standard message (subclass of [MessageContent]), such as [TextMessage].
Interface
IMCenter.getInstance().sendMessage(message, pushContent, pushData, callback);
Parameters
Parameter | Type | Description |
---|---|---|
message | [Message] | The message object. See [Message Introduction] for structure details. |
pushContent | String | Customizes push notification content. Can also be configured via MessagePushConfig in the message (overrides this parameter). See Custom Push Notifications.
|
pushData | String | Additional push payload accessible via io.rong.push.notification.PushNotificationMessage#getPushData() . Can also be configured via MessagePushConfig . |
callback | [ISendMessageCallback] | Message delivery callback |
Example
String targetId = "conversation ID";
ConversationType conversationType = ConversationType.PRIVATE;
TextMessage messageContent = TextMessage.obtain("Message content");
Message message = Message.obtain(targetId, conversationType, messageContent);
IMCenter.getInstance().sendMessage(message, null, null, new IRongCallback.ISendMessageCallback() {
@Override
public void onAttached(Message message) {}
@Override
public void onSuccess(Message message) {}
@Override
public void onError(Message message, RongIMClient.ErrorCode errorCode) {}
});
The sendMessage
method provides direct parameters for push content (pushContent
) and push payload (pushData
). For advanced push notification customization (e.g., title, icon, or platform-specific configurations), use MessagePushConfig
instead (see Custom Push Notifications). Note: Chatrooms don't support offline messages or push notifications.
- For SDK built-in message types like [TextMessage], these parameters can be
null
. RC server will use default push content when needed (see [User Content Message Formats]). - Custom message types requiring offline push must provide
pushContent
either here or viaMessagePushConfig
. - Business logic messages (e.g., commands) that don't need push can leave
pushContent
empty. MessagePushConfig
configurations override these parameters and offer more options (e.g., custom titles). See [Custom Push Notifications].
Sending Media Messages
For media messages, the content
field must contain a [MediaMessageContent] subclass like [ImageMessage] or [GIFMessage]. Other built-in media types include [FileMessage], LocationMessage
, [HQVoiceMessage], and [SightMessage] (require respective plugins).
[ImageMessage] supports sending original/uncompressed images:
String targetId = "Target ID";
ConversationType conversationType = ConversationType.PRIVATE;
Uri localUri = Uri.parse("file://image_path"); // Local image path
boolean mIsFull = true; // Send original image
ImageMessage mediaMessageContent = ImageMessage.obtain(localUri, mIsFull);
Message message = Message.obtain(targetId, conversationType, mediaMessageContent);
Use sendMediaMessage
for media messages. The SDK generates thumbnails, compresses media (per default compression settings), uploads to RC's file server (storage duration), then sends the message. Original images (when enabled) bypass compression.
IMCenter.getInstance().sendMediaMessage(message, null, null, new IRongCallback.ISendMediaMessageCallback() {
@Override
public void onProgress(Message message, int progress) {}
@Override
public void onCanceled(Message message) {}
@Override
public void onAttached(Message message) {}
@Override
public void onSuccess(Message message) {}
@Override
public void onError(Message message, RongIMClient.ErrorCode errorCode) {}
});
Push notification parameters work the same as standard messages (see above). Chatrooms don't support offline push.
Parameter | Type | Description |
---|---|---|
message | [Message] | The message object with content as a [MediaMessageContent] subclass. See [Message Introduction] for structure details. |
pushContent | String | Custom push content. Overridable by MessagePushConfig . Required for offline push on custom message types. |
pushData | String | Additional push payload. Overridable by MessagePushConfig . |
callback | [ISendMediaMessageCallback] | Media message delivery callback |
Sending Media Messages with Custom Upload
For apps handling their own media uploads (e.g., to private servers), use sendMediaMessage
with [ISendMediaMessageCallbackWithUploader]. Implement upload logic in onAttached
and notify the SDK upon completion.
Interface
RongIM.getInstance().sendMediaMessage(message, null, null, callback);
Parameters
Parameter | Type | Description |
---|---|---|
message | [Message] | The message object |
pushContent | String | Custom push content. Overridable by MessagePushConfig . Required for offline push on custom message types. |
pushData | String | Additional push payload. Overridable by MessagePushConfig . |
callback | [ISendMediaMessageCallbackWithUploader] | Callback for custom uploads. MediaMessageUploader references [IRongCoreCallback.MediaMessageUploader]. |
Example
String path = "file://image_path";
Uri localUri = Uri.parse(path);
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "conversation ID";
ImageMessage imageMessage = ImageMessage.obtain(localUri, localUri);
Message message = Message.obtain(targetId, conversationType, imageMessage);
RongIM.getInstance().sendMediaMessage(message, null, null, new IRongCallback.ISendMediaMessageCallbackWithUploader() {
@Override
public void onAttached(Message message, final MediaMessageUploader uploader) {
/* Upload to your server */
uploadImg(imgMsg.getPicFilePath(), new UploadListener() {
@Override
public void onSuccess(String url) {
uploader.success(Uri.parse(url)); // Notify SDK with remote URL
}
@Override
public void onProgress(float progress) {
uploader.update((int) progress); // Update progress
}
@Override
public void onFail() {
uploader.error(); // Handle failure
}
});
}
@Override
public void onError(Message message, RongIMClient.ErrorCode errorCode) {
// Handle send error
}
@Override
public void onSuccess(Message message) {
// Handle success
}
@Override
public void onProgress(Message message, int progress) {
// Track progress
}
});
Sending Targeted Standard Messages
Use sendDirectionalMessage
to send messages to specific users in a group (others won't receive them). Construct the message content directly without a Message
object.
Interface
IMCenter.getInstance().sendDirectionalMessage(conversationType, targetId, messageContent, userIds, pushContent, pushData, callback);
Parameters
Parameter | Type | Description |
---|---|---|
conversationType | [ConversationType] | Must be ConversationType.GROUP |
targetId | String | Conversation ID |
messageContent | [MessageContent] | Message content (standard types only) |
userIds | String[] | Recipient user IDs |
pushContent | String | Custom push content. Overridable by MessagePushConfig . Required for offline push on custom message types. |
pushData | String | Additional push payload. Overridable by MessagePushConfig . |
callback | [IRongCallback.ISendMessageCallback] | Delivery callback |
Sending Targeted Media Messages
Similar to standard messages but requires [MediaMessageContent] as content.
Interface
IMCenter.getInstance().sendDirectionalMessage(conversationType, targetId, messageContent, userIds, pushContent, pushData, callback);
Parameters
Parameter | Type | Description |
---|---|---|
conversationType | [ConversationType] | Must be ConversationType.GROUP |
targetId | String | Conversation ID |
messageContent | [MediaMessageContent] | Media message content |
userIds | String[] | Recipient user IDs |
pushContent | String | Custom push content. Overridable by MessagePushConfig . Required for offline push on custom message types. |
pushData | String | Additional push payload. Overridable by MessagePushConfig . |
callback | [IRongCallback.ISendMediaMessageCallback] | Delivery callback |
Custom Push Notifications
Configure per-message push behavior via [MessagePushConfig] (intercept messages to apply settings). This overrides sendMessage
/sendMediaMessage
parameters and supports:
- Custom titles/content
- Notification icons
- Additional payload
- Force-showing notification content
- Platform-specific configurations
Message message = Message.obtain(mTargetId, mConversationType, textMessage);
MessagePushConfig messagePushConfig = new MessagePushConfig.Builder()
.setPushTitle(title)
.setPushContent(content)
.setPushData(data)
.setForceShowDetailContent(forceDetail)
.setAndroidConfig(new AndroidConfig.Builder()
.setNotificationId(id)
.setChannelIdHW(hw)
.setCategoryHW("IM")
.setChannelIdMi(mi)
.setChannelIdOPPO(oppo)
.setTypeVivo(vivo ? AndroidConfig.SYSTEM : AndroidConfig.OPERATE)
.setCategoryVivo("IM")
.build())
.setIOSConfig(new IOSConfig(threadId, apnsId))
.setTemplateId("")
.build();
message.setMessagePushConfig(messagePushConfig);
// Call appropriate send method based on message type
MessagePushConfig Properties
Parameter | Type | Description |
---|---|---|
disablePushTitle | boolean | Hide push title (iOS only; Android requires titles) |
pushTitle | String | Highest-priority push title. Falls back to defaults per [User Content Message Formats]. |
pushContent | String | Highest-priority push content. Falls back to defaults. |
pushData | String | Additional push payload. Overrides sendMessage parameters. |
forceShowDetailContent | boolean | Override client settings to always show notification content |
iOSConfig | IOSConfig | iOS-specific configurations (see below) |
androidConfig | AndroidConfig | Android-specific configurations (see below) |
templateId | String | Push template ID matching user's language setting (configured via RongIMClient.setPushLanguageCode ). Falls back to defaults if no match. Configure templates in Console > Custom Push Content. See Configuring Multilingual Push Templates. |
-
iOSConfig
PropertiesParameter Type Description threadId String Notification grouping ID (iOS10+) apnsCollapseId String Notification replacement ID (same IDs overwrite previous notifications, iOS10+) richMediaUri String Custom notification icon URL (right side). App must handle display. Requirements: 120x120px, PNG/JPG. Supported since SDK 5.2.4. interruptionLevel String iOS15+: passive
,active
(default),time-sensitive
, orcritical
. See APNs interruption-level. Helps bypass "Scheduled Summary" and "Focus Mode". Supported since SDK 5.6.7. -
AndroidConfig
PropertiesParameter Type Description notificationId String Android push unique ID (defaults to messageUId
). Supported by Xiaomi/Huawei.channelIdMi String Xiaomi notification channel ID. See Xiaomi Push Categories. imageUrlMi String (Deprecated) Xiaomi notification image URL (120x120px PNG/JPG). Supported since SDK 5.1.7 for MIUI 12+ (China/Global). channelIdHW String Huawei notification channel ID. See Huawei Custom Channels and Android Docs. imageUrlHW String Huawei notification icon URL (right side). Max 512KB, recommended 40dp x 40dp with 8dp corners. Supported since SDK 5.1.7. importanceHW String Huawei notification priority: LOW
(silent) orNORMAL
(sound/vibration). Actual behavior depends oncategoryHW
or console settings. Supported since SDK 5.1.3.categoryHW String Huawei message category (uppercase, e.g., IM
). Requires [Huawei Category Approval] or [Special Permissions]. Overrides console category settings. See [Huawei Category Standards]. Supported since SDK 5.4.0.imageUrlHonor String Honor notification icon URL (right side). Max 512KB, recommended 40dp x 40dp with 8dp corners. Supported since SDK 5.6.7. importanceHonor String Honor notification category: LOW
(marketing) orNORMAL
(service/communication, default). Supported since SDK 5.6.7.typeVivo String VIVO notification type: 0
(operational) or1
(system). Maps toclassification
in [VIVO Push Docs].categoryVivo String VIVO sub-category (e.g., IM
). Requires matchingtypeVivo
. Must comply with VIVO's allowed categories. Overrides console settings. See [VIVO Push Docs]. Supported since SDK 5.4.2.channelIdOPPO String OPPO notification channel ID. See OPPO Push Upgrade. channelIdFCM String FCM notification channel ID. App must create channel first. See Android Docs. collapseKeyFCM String FCM notification group ID. Requires FCM console configuration as Notification Messages. Supported since SDK 5.1.3. imageUrlFCM String FCM notification icon URL. Requires FCM console Certificate auth and Notification Messages mode. Supported since SDK 5.1.3.
Disabling Push Notifications
By default, RC server triggers push notifications when recipients are offline (subject to app/user-level DND settings). To disable push for specific messages:
-
Implement [MessageInterceptor] and intercept messages via
interceptOnSendMessage
(returntrue
). See [Message Interception]. -
Set
disableNotification
inmessageConfig
totrue
:message.setMessageConfig(new MessageConfig.Builder().setDisableNotification(true).build());
-
Resend the message.
[Message Interception