Skip to main content

Receiving Messages

You can intercept messages received by the SDK through delegate protocols or notification listeners for corresponding business operations.

Message Reception Delegate Protocol

The IMKit SDK provides the RCIMReceiveMessageDelegate protocol to receive real-time or offline messages.

tip
  • This protocol offers two delegate methods. Implementing either one suffices for message handling.
  • The SDK uses this method to receive all message types including one-to-one chat, group chat, chatroom, and system messages.
  • Set this delegate to the singleton object after SDK initialization but before connecting to IM.

Interface Prototype

/*!
Callback for received messages

@param message The currently received message
@param nLeft Number of remaining unreceived messages (left≥0)

*/
- (void)onRCIMReceiveMessage:(RCMessage *)message left:(int)left;

Compared to the first method, the second delegate exposes additional offline and hasPackage parameters. If you implement custom UI refresh logic, use nLeft, offline, and hasPackage to determine the optimal refresh timing. We recommend refreshing the UI when hasPackage=0 and nLeft=0. If using this method, do not simultaneously implement RCIM's - (void)onRCIMReceived:(RCMessage *)message left:(int)nLeft to avoid duplicate operations.

/**
Callback for received messages

@param message The currently received message
@param nLeft Number of remaining unreceived messages (left≥0)
@param offline Whether it's an offline message
@param hasPackage The SDK pulls server messages in packages. A package existence indicates pending messages on the remote server not yet pulled by the SDK
*/
- (void)onRCIMReceived:(RCMessage *)message
left:(int)nLeft
offline:(BOOL)offline
hasPackage:(BOOL)hasPackage;

Parameter Description

ParameterTypeDescription
messageRCMessageThe received message object.
nLeftintAfter successful connection, the server delivers all offline messages? in packages (max 200 messages per package). The SDK parses these messages sequentially. nLeft indicates remaining messages in the current package being parsed.
offlinebooleanWhether the current message is an offline message.
hasPackagebooleanWhether undispatched message packages remain on the server.

Adding Message Reception Delegates

The SDK supports multiple message reception delegates. Set them to the singleton object after SDK initialization but before connecting to IM.

[[RCIM sharedRCIM] addReceiveMessageDelegate:self];

Removing Message Reception Delegates

The SDK allows delegate removal. Remove listeners when unnecessary to prevent memory leaks.

[[RCIM sharedRCIM] removeReceiveMessageDelegate:self];

Notification Listening

Developers can listen for message reception via notifications at any time.

Upon receiving messages, the SDK dispatches the RCKitDispatchMessageNotification.

FOUNDATION_EXPORT NSString *const RCKitDispatchMessageNotification;
  1. Register message reception notification
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didReceiveMessageNotification:)
name:RCLibDispatchReadReceiptNotification
object:nil];
  1. Implement message reception handler
- (void)didReceiveMessageNotification:(NSNotification *)notification {
RCMessage *message = notification.object;
int left = [[notification.userInfo objectForKey:@"left"] intValue];
}

Parameter Description

Notification's object is the RCMessage object. userInfo is an NSDictionary. userInfo dictionary description:

KeyValue TypeDescription
leftNSNumberNumber of remaining messages to receive

Disabling Message Deduplication

The deduplication mechanism automatically filters duplicate messages in one-to-one chats, group chats, system messages, and chatrooms. When an app stores numerous messages locally, SDK's default deduplication may cause message reception lag due to performance issues. If message reception experiences delays, consider disabling the SDK's deduplication.

Why Duplicate Messages May Occur

This typically happens when the sender has poor network connectivity. When UserA sends a message to UserB, the message reaches the server and gets delivered to UserB successfully. However, if UserA doesn't receive the server's acknowledgment due to network issues, UserA may resend the message (IMKit enables auto-resend by default - we recommend disabling this simultaneously). Consequently, UserB receives duplicate messages (same content but different Message UIDs).

Disabling Deduplication

For one-to-one/group/system messages, use IMLib's RCCoreClient class method setCheckDuplicateMessage (requires SDK version ≥ 5.3.4). Configure this after SDK initialization but before IM connection:

BOOL enableCheck = NO; // Disable deduplication
[[RCCoreClient sharedCoreClient] setCheckDuplicateMessage:enableCheck];

For chatroom messages, use RCChatRoomClient's setCheckChatRoomDuplicateMessage (requires SDK version ≥ 5.8.2). Configure this after SDK initialization but before IM connection:

BOOL enableCheck = NO; // Disable deduplication
[[RCChatRoomClient sharedChatRoomClient] setCheckChatRoomDuplicateMessage:enableCheck];

Disabling Auto-Resend

IMKit SDK enables message auto-resend by default. After disabling deduplication, we recommend disabling IMKit's auto-resend to prevent receiving duplicate messagUId messages.

Configure this after SDK initialization but before IM connection:

RCKitConfigCenter.message.enableMessageResend = NO;