Skip to main content

Intercept messages

The Chat UI SDK allows intercepting messages before sending, after sending, and upon receiving.

The message interception feature provides these protocols:

  • NCChatUIMessageInterceptor: Supports intercepting messages before and after sending.
  • NCChatUIMessagePolicyDelegate: Supports deciding whether to intercept received messages, mute alert sounds, or suppress local notifications.
tip

The Chat UI channel page (NCChannelViewController) also provides willSendMessage: and didSendMessageModel: methods for message interception. See Page event listeners for details.

Intercept messages before and after sending

The NCChatUIMessageInterceptor protocol provides callback methods to intercept messages before and after sending.

Typical use cases for intercepting messages during sending:

  • Modify regular message parameters before sending, then let the SDK continue sending (return NO to continue SDK sending)
  • Modify media message parameters before sending, then have the app resend (return YES to intercept and handle manually)
  • Intercept media messages to upload attachments to a custom file server before resending
  • Intercept after sending to perform business logic based on the send result

Set the delegate

Set the NCChatUIMessageInterceptor delegate globally once. Multiple settings will override previous delegates.

Objective C
[NCChatUI shared].messageInterceptor = self;

Implement these protocol methods:

Objective C
#pragma mark -- NCChatUIMessageInterceptor

- (BOOL)interceptWillSendMessageWithParams:(NCChatUISendMessageParams *)params {
return NO;
}

- (BOOL)interceptWillSendMediaMessageWithParams:(NCChatUISendMediaMessageParams *)params {
// Return YES to intercept and handle sending manually
return NO;
}

- (void)interceptDidSendMessage:(NCMessage *)message {
}

Intercept received messages

The NCChatUIMessagePolicyDelegate protocol provides methods to intercept received messages.

After setting the delegate, implement this method. Return YES to intercept the received message.

Objective C
@interface AppDelegate () <NCChatUIMessagePolicyDelegate>
@end

- (void)setupMessagePolicy {
[NCChatUI shared].messagePolicyDelegate = self;
}

- (BOOL)shouldInterceptMessage:(NCMessage *)message {
return NO;
}

Control alert sounds and local notifications

NCChatUIMessagePolicyDelegate also supports controlling alert sounds and local notifications:

Objective C
- (BOOL)shouldSuppressAlertSoundForMessage:(NCMessage *)message {
return NO;
}

- (BOOL)shouldSuppressLocalNotificationForMessage:(NCMessage *)message
senderName:(NSString *)senderName {
return NO;
}