Skip to main content

Delete Message

After a user successfully sends a message, they may find that the message content is incorrect and wish to delete it.

Usage

Global IM UIKit enables the delete feature by default. On the chat UI, a user can long-press a successfully sent message to open a pop-up window and click Delete. The options available may include Delete on my device or Delete on all devices. After deleting on all devices, the message can be re-edited within a certain time frame.

alt(width=250) alt(width=250)

Global IM UIKit implements the message delete feature as shown above:

  • Delete on my device: Deletes the message from the user’s local message database. If message cloud storage is enabled, the corresponding message in the user’s cloud history will also be deleted.
  • Delete on all devices: Removes the message from the sender’s (current user) and all recipients’ message records, i.e., recalls the message. By default, this option is available within 120 seconds after the message is sent. After the time limit, this option is no longer displayed. After a successful deletion, the IMLib SDK replaces the original message in the local message database with a recall notification message (RecallNotificationMessage). If message cloud storage is enabled, the corresponding message in the sender’s and all recipients’ cloud history will also be deleted.

Customization

Modify the Time Limit for Deleting on All Devices

Global IM UIKit allows deleting a message on all devices within 120 seconds after sending by default. You can adjust this limit via global configuration.

ConfigCenter.getChatConfig().getMessageDeleteOnAllDevicesInterval(120);

Handle the Re-edit Button Click Event

  1. Create a subclass by extending Global IM UIKit’s MessageListComponent and override the beforeCreateMsgActionView method. For example, CustomMessageListComponent.

    public class CustomMessageListComponent extends MessageListComponent {

    public CustomMessageListComponent(Context context) {
    super(context);
    }

    public CustomMessageListComponent(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    }

    @Override
    protected List<MsgAction<UiMessage>> beforeCreateMsgActionView(List<MsgAction<UiMessage>> msgActions) {
    // 1. Remove the delete button
    Iterator<MsgAction<UiMessage>> iterator = msgActions.iterator();
    while (iterator.hasNext()) {
    MsgAction<UiMessage> msgAction = iterator.next();
    if (msgAction instanceof DeleteMsgAction) {
    iterator.remove();
    }
    }

    // 2. Add a custom CustomMsgAction to the specified position
    msgActions.add(0, new CustomMsgAction());

    return msgActions;
    }
    }
  2. Copy Global IM UIKit’s rc_chat_module.xml to the main project’s res/layout directory, locate <com.rongcloud.im.uikit.chat.component.MessageListComponent />, and replace it with the subclass’s full path <com.rongcloud.im.uikit.custom.component.CustomMessageListComponent />.

    Before:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.rongcloud.im.uikit.chat.component.MessageListComponent
    android:id="@+id/rc_message_list_component"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@id/fl_bottom"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/fl_header" />

    </FrameLayout>

    After:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.rongcloud.im.uikit.custom.component.CustomMessageListComponent
    android:id="@+id/rc_message_list_component"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@id/fl_bottom"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/fl_header" />

    </FrameLayout>

If the existing implementation does not meet your requirements, you can use the relevant APIs in the IMLib SDK. Refer to the following documentation:

Permissions for Deleting Messages on All Devices

danger

By default, the IM Service does not restrict the operator of the "Delete on all devices" action. This means that if you customize the UI, you can allow admin users in your app to recall messages sent by others. However, improper implementation may also allow anyone to recall messages sent by others.

When customizing the delete message feature in Global IM UIKit, if you need to restrict permissions for deleting messages on all devices, consider the following solutions:

  • Override the DeleteMsgAction#filter(@NonNull UiMessage uiMessage) method in the app client. Refer to Handle the Re-edit Button Click Event to determine whether a user can delete a message based on business requirements.
  • To prevent users from recalling messages sent by others, you can submit a ticket to enable the IMLib SDK only allows recalling messages sent by the user. This restriction is enforced on the RC server to prevent users from recalling messages sent by others.