Message Interception
The IMKit SDK allows intercepting messages before sending, after sending, and upon receipt.
Message interception provides the following protocols:
- RCIMMessageInterceptor (requires SDK ≧ 5.3.5): Supports intercepting messages before and after sending, with access to the message object (RCMessage).
RCIMSendMessageDelegate
(deprecated): Supports intercepting messages before and after sending, but only provides access to thecontent
field of RCMessage, i.e., subclasses of RCMessageContent or RCMediaMessageContent. Deprecated as of SDK 5.3.5.- RCIMReceiveMessageDelegate: Supports intercepting messages in real-time upon receipt.
The conversation page (RCConversationViewController
) in IMKit also provides message interception-related methods willSendMessage:
and didSendMessage:content:
. For details, see Page Event Listeners.
Intercept Messages Before and After Sending (SDK ≧ 5.3.5)
Through the callback methods provided by the RCIMMessageInterceptor
protocol, you can intercept messages before and after sending, with access to the RCMessage object.
Possible use cases for intercepting messages before and after sending include:
- Intercept and modify
RCMessage
before sending, then let the SDK continue sending (note: for 5.3.5 ≦ SDK < 5.4.5, this only supports modifying thecontent
field ofRCMessage
; for SDK ≧ 5.4.5, modifyingRCMessage
is fully supported). In this case, returnNO
to indicate the app needs to intercept but requires the SDK to send the modifiedRCMessage
. - Intercept and modify
RCMessage
before sending, then let the app resend. This fully supports modifyingRCMessage
. ReturnYES
to indicate the app will handle the subsequent process and resend the modified message. - Intercept
RCMessage
before sending, upload media files to the app's designated file server, then let the app resend. ReturnYES
to indicate the app will handle the subsequent process. Note: The app must call the custom file server upload method. See Sending Messages. - Intercept after sending to modify certain attributes of the
RCMessage
object in the database, such as additional info viasetMessageExtra
. See methods in RCCoreClient or RCChannelClient. Note: You may need to check the message's sent status (RCMessage.sentStatus
) before deciding the next steps.
Setting the Delegate
The RCIMMessageInterceptor delegate only needs to be set once globally. Multiple settings will override previous delegates.
[RCIM sharedRCIM].messageInterceptor = self;
To implement this, your class must conform to the RCIMMessageInterceptor protocol.
#pragma mark -- RCIMMessageInterceptor
- (BOOL)interceptWillSendMessage:(RCMessage *)message {
if ([message.content isKindOfClass:[RCCombineMessage class]]) {
// Example 1: Intercept combined-forwarded messages, let the app handle sending
[[RCIM sharedRCIM] sendMediaMessage:message pushContent:nil pushData:nil uploadPrepare:^(RCUploadMediaStatusListener *uploadListener) {
RCCombineMessage *msgContent = (RCCombineMessage *)uploadListener.currentMessage.content;
msgContent.remoteUrl = @"https://your_file_server.com.com/test.html";
uploadListener.successBlock(msgContent);
} progress:nil successBlock:nil errorBlock:nil cancel:nil];
// Return YES to let the app handle sending
return YES;
}
else if ([message.content isKindOfClass:[RCImageMessage class]]) {
// Example 2.1: Filter out unintercepted image messages (customize conditions as needed)
RCImageMessage *msgContent = (RCImageMessage *)message.content;
if ([msgContent.remoteUrl containsString:@"your_file_server"]) {
// Image already uploaded to the app's file server; no need to intercept
return NO;
}
// Example 2.2: Intercept image messages, let the app handle sending
[[RCIM sharedRCIM] sendMediaMessage:message pushContent:nil pushData:nil uploadPrepare:^(RCUploadMediaStatusListener *uploadListener) {
RCImageMessage *msgContent = (RCImageMessage *)uploadListener.currentMessage.content;
// Implement custom file upload logic
// Assign the uploaded file URL to remoteUrl
msgContent.remoteUrl = @"http://your_file_server.com/test.jpg";
uploadListener.successBlock(msgContent);
} progress:nil successBlock:nil errorBlock:nil cancel:nil];
// Return YES to let the app handle sending
return YES;
}
else if ([message.content isKindOfClass:[RCTextMessage class]]) {
// Example 3: Intercept and let the SDK continue sending after updating text content
RCTextMessage *msgContent = (RCTextMessage *)message.content;
msgContent.content = @"Text content has been replaced";
// Return NO to let the SDK handle sending
return NO;
}
return NO;
}
Intercept Received Messages
The RCIMReceiveMessageDelegate protocol in IMKit provides methods for intercepting received messages.
For delegate setup, see Message Listeners. After setting the delegate, implement the interceptMessage:
method. Return YES
to intercept the received message, preventing further callbacks.
This only determines whether the message is displayed immediately upon receipt. Returning YES
intercepts the message, but reloading the conversation page will still display it. To prevent this, delete the message. See Deleting Messages.
@protocol RCIMReceiveMessageDelegate <NSObject>
/*!
Callback when Kit receives a message
@param message The received message
@return YES: Intercept (do not display); NO: Do not intercept (display the message).
*/
- (BOOL)interceptMessage:(RCMessage *)message;
@end
Intercept Messages Before and After Sending (Deprecated)
The RCIMSendMessageDelegate
protocol has been deprecated as of SDK 5.3.5.
When IMKit sends a message, you can intercept it via delegate listeners. Set the delegate only once globally.
Your class must conform to the RCIMSendMessageDelegate protocol.
Set the delegate:
[RCIM sharedRCIM].sendMessageDelegate = self;