Skip to main content

Message Modification

This document explains how to use the message modification feature in IMLib SDK.

tip

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

ParameterTypeDescription
paramsModifyMessageParamsMessage modification parameter object
callbackIRongCoreCallback.ModifyMessageCallbackInterface callback

ModifyMessageParams Property Description

PropertyTypeDescription
messageUIdStringUnique message ID.
messageContentMessageContentModified 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 ValueMethodDescription
voidonComplete(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

ParameterTypeDescription
paramsRefreshReferenceMessageParamsParameter information
callbackIRongCoreCallback.RefreshReferenceMessageCallbackInterface callback

RefreshReferenceMessageParams Property Description

PropertyTypeDescription
conversationIdentifierConversationIdentifierConversation identifier.
messageUIdsList<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 ValueMethodDescription
voidonLocalMessageBlock(msgList)Callback for locally existing messages.
voidonRemoteMessageBlock(msgList)For messages not found locally, remote messages will be automatically loaded and the result will be returned.
voidonError(errorCode)Failure callback.

MessageResult Property Description

PropertyTypeDescription
messageUIdStringQueried message UID.
messageMessageRetrieved message.
codeIRongCoreEnum.CoreErrorCodeError 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

tip

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) {
}
});