Message Modification
This document explains how to use the message modification feature in IMLib SDK.
This feature is supported starting from version 5.26.0.
Message Modification Overview:
After successfully sending a message through IMLib SDK, if you find the message content contains errors, you can use the message modification feature to correct it. The modified content will be synchronized to the recipient's message history. The message modification interface supports all stored message types (text, images (including GIF), voice (HD, standard), video, reference, files, and custom messages).
Message Modification Use Cases:
- In private chats: Users can edit the content of sent messages after successful delivery.
- In group chats: Users can edit the content of sent messages after successful delivery, or group administrators can edit specified message content.
Adding a Message Modification Listener
After the sender modifies a message, the recipient can listen for message modification events by adding a message modification callback interface and handle them accordingly.
The recipient needs to use addMessageModifiedListener to add a listener for monitoring events when received messages are modified. This listener callback will be triggered when any received message is modified.
RongCoreClient.getInstance().addMessageModifiedListener(new IRongCoreListener.MessageModifiedListener() {
@Override
public void onMessageModified(List<Message> messages) {
// This callback is triggered when a received message is modified.
}
@Override
public void onModifiedMessageSyncCompleted() {
// This callback is triggered when offline message modification records are fully synchronized.
}
});
Removing the Message Modification Listener
To prevent memory leaks, call removeMessageModifiedListener to remove the listener when it's no longer needed.
RongCoreClient.getInstance().removeMessageModifiedListener(listener);
Modifying Messages
You can modify sent messages by calling the modifyMessageWithParams interface.
RongCoreClient.getInstance().modifyMessageWithParams(params,callback);
Parameter Description
| Parameter | Type | Description |
|---|---|---|
params | ModifyMessageParams | Message modification parameter object |
callback | IRongCoreCallback.ModifyMessageCallback | Interface callback |
ModifyMessageParams Property Description
| Property | Type | Description |
|---|---|---|
messageUId | String | Unique message ID. |
messageContent | MessageContent | Modified message body. |
IRongCoreCallback.ModifyMessageCallback Parameter Description
When calling the interface to modify a message, the result will be returned via the onComplete method of IRongCoreCallback.ModifyMessageCallback.
| Return Value | Method | Description |
|---|---|---|
void | onComplete(errorCode,message) | This callback is always triggered, regardless of success or failure. On success, message contains the modified message. On failure, message contains the original message. If the SDK is uninitialized or parameter validation fails, message is NULL. |
Example Code
// Assume originalMessage is the message to be modified
MessageContent content = originalMessage.getContent();
// Modify the message body
if (content instanceof TextMessage) {
((TextMessage)content).setContent("Modified message body");
}
ModifyMessageParams params = new ModifyMessageParams(originalMessage.getUId(),content);
RongCoreClient.getInstance().modifyMessageWithParams(params, new IRongCoreCallback.ModifyMessageCallback() {
@Override
public void onComplete(IRongCoreEnum.CoreErrorCode errorCode, Message message) {
// Handle the result
}
});
Batch Query for Reference Messages Needing Refresh
Since ReferenceMessage caches the content of the original text message, modifying the original text message does not automatically update the cached content in the reference message. When displaying reference messages, you can call the refreshReferenceMessageWithParams interface to refresh the reference message, updating the cached text content to the latest message content.
Interface
RongCoreClient.getInstance().refreshReferenceMessageWithParams(params,callback);
Parameter Description
| Parameter | Type | Description |
|---|---|---|
params | RefreshReferenceMessageParams | Parameter information |
callback | IRongCoreCallback.RefreshReferenceMessageCallback | Interface callback |
RefreshReferenceMessageParams Property Description
| Property | Type | Description |
|---|---|---|
conversationIdentifier | ConversationIdentifier | Conversation identifier. |
messageUIds | List<String> | Array of reference message unique IDs, up to 20. |
IRongCoreCallback.RefreshReferenceMessageCallback Parameter Description
When calling the batch query interface for reference messages needing refresh, the result will be returned via IRongCoreCallback.RefreshReferenceMessageCallback.
| Return Value | Method | Description |
|---|---|---|
void | onLocalMessageBlock(msgList) | Callback for locally existing messages. |
void | onRemoteMessageBlock(msgList) | For messages not found locally, remote messages will be automatically loaded and the result will be returned. |
void | onError(errorCode) | Failure callback. |
MessageResult Property Description
| Property | Type | Description |
|---|---|---|
messageUId | String | Queried message UID. |
message | Message | Retrieved message. |
code | IRongCoreEnum.CoreErrorCode | Error code. |
Example Code
RefreshReferenceMessageParams params = new RefreshReferenceMessageParams(conversationIdentifier,messageUIds);
RongCoreClient.getInstance().refreshReferenceMessageWithParams(params, new IRongCoreCallback.RefreshReferenceMessageCallback() {
@Override
public void onLocalMessageBlock(List<MessageResult> msgList) {
for (MessageResult messageResult : msgList) {
if (messageResult.getCode() == IRongCoreEnum.CoreErrorCode.SUCCESS) {
Message message = messageResult.getMessage();
// Refresh UI
// ......
}
}
}
@Override
public void onRemoteMessageBlock(List<MessageResult> msgList) {
for (MessageResult messageResult : msgList) {
if (messageResult.getCode() == IRongCoreEnum.CoreErrorCode.SUCCESS) {
Message message = messageResult.getMessage();
// Refresh UI
// ......
}
}
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode errorCode) {
// Error handling
}
});
Setting Conversation Draft for Editing
Setting Conversation Draft for Editing
This feature is supported starting from version 5.28.0.
When a user is editing a message, you can use the saveEditedMessageDraft method to save the draft. This allows users to continue editing the message later, improving the user experience.
Interface
RongCoreClient.getInstance().saveEditedMessageDraft(draft, identifier, callback);
#### Parameters
| Parameter | Type | Description |
|:------------|:----------------------------------------|:-----------|
| `draft` | `EditedMessageDraft` | Draft information containing message UID and draft content |
| `identifier`| `ConversationIdentifier` | Conversation identifier specifying the target conversation for draft saving |
| `callback` | `IRongCoreCallback.OperationCallback` | Callback interface returning operation results |
#### EditedMessageDraft Properties
| Property | Type | Description |
| :--------- | :-------- |:-----------|
| `messageUId` | `String` | Message UID identifying the message to be edited |
| `content` | `String` | Draft content - the text currently being edited by the user |
#### Sample Code
```java
EditedMessageDraft draft = new EditedMessageDraft("MSG_UID", "CONTENT");
ConversationIdentifier identifier = ConversationIdentifier.obtain(Conversation.ConversationType.PRIVATE, "Target_ID", "");
RongCoreClient.getInstance().saveEditedMessageDraft(draft, identifier, new IRongCoreCallback.OperationCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode code) {
}
});
## Querying Conversation Draft
:::tip
This feature is supported starting from version 5.28.0.
:::
You can use the `getEditedMessageDraft` method to retrieve saved drafts in a specified conversation. When users return to the editing page, they can restore previously saved draft content.
#### Interface
```java
RongCoreClient.getInstance().getEditedMessageDraft(identifier, callback);
#### Parameters
| Parameter | Type | Description |
|:------------|:----------------------------------------|:-----------|
| `identifier`| `ConversationIdentifier` | Conversation identifier specifying the target conversation to query for drafts |
| `callback` | `IRongCoreCallback.ResultCallback` | Callback interface returning the queried draft information |
#### Sample Code
```java
ConversationIdentifier identifier = ConversationIdentifier.obtain(Conversation.ConversationType.PRIVATE, "Target_ID", "");
RongCoreClient.getInstance().getEditedMessageDraft(identifier, new IRongCoreCallback.ResultCallback<EditedMessageDraft>() {
@Override
public void onSuccess(EditedMessageDraft draft) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode code) {
}
});
## Deleting Conversation Draft
:::tip
This feature is supported starting from version 5.28.0.
:::
When users complete message editing or no longer need the draft, you can use the `clearEditedMessageDraft` method to delete the editing draft for a specified conversation, freeing up storage space.
#### Interface
```java
RongCoreClient.getInstance().clearEditedMessageDraft(identifier, callback);
#### Parameters
| Parameter | Type | Description |
|:------------|:----------------------------------------|:-----------|
| `identifier`| `ConversationIdentifier` | Conversation identifier specifying the target conversation for draft deletion |
| `callback` | `IRongCoreCallback.OperationCallback` | Callback interface returning deletion operation results |
#### Sample Code
```java
ConversationIdentifier identifier = ConversationIdentifier.obtain(Conversation.ConversationType.PRIVATE, "Target_ID", "");
RongCoreClient.getInstance().clearEditedMessageDraft(identifier, new IRongCoreCallback.OperationCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode code) {
}
});