Receive messages
You can listen to messages received by the SDK through message event observers or message policy delegates, then perform corresponding business operations.
Message event observer protocol
The Chat UI SDK provides the NCChatUIMessageEventObserver protocol to receive real-time or offline messages.
tip
- Set this on the singleton object after SDK initialization but before connecting to IM.
- The SDK receives all message types through this protocol, including direct channels, group channels, open channels, and system channels.
Interface prototype
Objective C
- (void)onReceivedMessage:(NCMessage *)message left:(int)left offline:(BOOL)offline;
Parameters
| Parameter | Type | Description |
|---|---|---|
| message | NCMessage | The received message object. |
| left | int | Number of remaining unreceived messages. |
| offline | BOOL | Whether the message is offline. |
Add message event observer
The SDK supports multiple message event observers. Set them on the singleton object after SDK initialization but before connecting to IM.
Objective C
[[NCChatUI shared] addMessageEventObserver:self];
Remove message event observer
The SDK supports removing observers. To avoid memory leaks, remove observers when no longer needed.
Objective C
[[NCChatUI shared] removeMessageEventObserver:self];
Intercept received messages
To intercept messages before they enter the UI, implement NCChatUIMessagePolicyDelegate:
Objective C
@interface AppDelegate () <NCChatUIMessagePolicyDelegate>
@end
- (void)setupMessagePolicy {
[NCChatUI shared].messagePolicyDelegate = self;
}
- (BOOL)shouldInterceptMessage:(NCMessage *)message {
return NO;
}
Return YES to intercept the received message.