Skip to main content

Message Interception

IMKit supports setting up a MessageInterceptor to intercept messages before sending, after sending, upon receiving, and before inserting into the local database, allowing for custom processing.

Message Interceptor Overview

MessageInterceptor is an interface class containing the following methods.

MethodDescription
interceptOnSendMessageIntercepts messages to be sent. This method can intercept Message objects.
interceptOnSentMessageIntercepts successfully sent messages. This method can intercept Message objects.
interceptOnInsertOutgoingMessageinterceptOnInsertOutgoingMessage intercepts outgoing message content before insertion into the local message database, with two overloaded methods:
  • The method supporting ResultCallback<Message> callback is used to intercept image and GIF messages sent from IMKit's built-in chat UI before insertion into the local database. See How to Intercept Image and GIF Messages Sent from IMKit's Built-in Chat UI below. Requires SDK ≧ 5.2.4.
  • The method without the callback parameter intercepts outgoing message content before insertion into the local message database. Note: Cannot be used to intercept image and GIF messages sent from IMKit's built-in chat UI.
interceptOnInsertIncomingMessageIntercepts incoming message content before insertion into the local message database.
interceptReceivedMessageIntercepts received messages. Offline messages are received first after SDK connection. hasPackage indicates whether there are remaining offline message packets, and left indicates how many messages remain in the current offline message packet being received. When hasPackage is false and left is 0, it means all offline messages have been received, and the current intercepted message is a real-time message. This method can intercept Message objects.

Returning false in any of the above methods indicates that the app only needs to intercept and process the message, with the SDK handling subsequent steps. Returning true indicates that the app needs to handle subsequent steps after interception, such as sending modified messages independently.

Setting Up the Message Interceptor

Use IMCenter's setMessageInterceptor to set up the message interceptor. Example code demonstrates:

  • Modifying push notification content in interceptOnSendMessage
  • Modifying image and GIF message extensions in interceptOnInsertOutgoingMessage
private void initInterceptor() {
IMCenter.getInstance().setMessageInterceptor(
new MessageInterceptor() {
@Override
public boolean interceptReceivedMessage(
Message message,
int left,
boolean hasPackage,
boolean offline) {
return false;
}

@Override
public boolean interceptOnSendMessage(Message message) {
MessageContent messageContent = message.getContent();
String language = mContext.getResources().getConfiguration().locale.getLanguage();
if (language.endsWith("en")) { // System language is English
if (messageContent instanceof VoiceMessage) {
MessagePushConfig messagePushConfig = message.getMessagePushConfig();
if (messagePushConfig == null){
messagePushConfig = new MessagePushConfig();
message.setMessagePushConfig(messagePushConfig);
}
messagePushConfig.setPushContent("[voice]"); // Voice messages display as "[voice]" in push notifications
}
}
return false;
}

@Override
public boolean interceptOnSentMessage(Message message) {
if (message != null) {
message.setMessageConfig(null);
}
return false;
}

@Override
public boolean interceptOnInsertOutgoingMessage(
Conversation.ConversationType type,
String targetId,
Message.SentStatus sentStatus,
MessageContent content,
long time) {
return false;
}

@Override
public boolean interceptOnInsertOutgoingMessage(
Conversation.ConversationType type,
String targetId,
Message.SentStatus sentStatus,
MessageContent content,
long time,
RongIMClient.ResultCallback<Message> callback) {
// When UI integration enables message expansion for image messages, return true here,
// set the message's expandable property to true, and call callback.onSuccess(message)
Message message = Message.obtain(targetId, type, content);
message.setCanIncludeExpansion(true);
callback.onSuccess(message);
return true;
}

@Override
public boolean interceptOnInsertIncomingMessage(
Conversation.ConversationType type,
String targetId,
String senderId,
Message.ReceivedStatus receivedStatus,
MessageContent content,
long time) {
return false;
}
});
}

How to Intercept Image and GIF Messages Sent from IMKit's Built-in Chat UI

tip

Requires SDK version ≧ 5.2.4.

When sending image or GIF messages from IMKit's built-in chat UI, the SDK first inserts the message into the local message database before sending it. To:

  • Set message extensions for image/GIF messages by enabling their expandable property (setCanIncludeExpansion(true)).
  • Modify message data like thumbnails or the extra field in the message content.
  • Completely intercept image/GIF messages for custom handling.

Intercept these messages before database insertion using:

default boolean interceptOnInsertOutgoingMessage(
Conversation.ConversationType type,
String targetId,
Message.SentStatus sentStatus,
MessageContent content,
long time,
RongIMClient.ResultCallback<Message> callback)

After processing:

  • To let the SDK continue sending the message, call callback.onSuccess(message) and return true in interceptOnInsertOutgoingMessage (otherwise duplicate messages will be sent).
  • To fully intercept, call callback.onError(IRongCoreEnum.CoreErrorCode.PARAMETER_ERROR) and return true.