Skip to main content

Intercept messages

The current version recommends implementing message interception and secondary processing through event listening + send wrapping.

1. Receiving-side interception (listening)

Use NCEngine.addMessageHandler(...) to listen for message received, deleted, modified, and other events. Your business layer decides whether and how to display them.

kotlin
private const val MESSAGE_HANDLER_ID = "app_message_interceptor"

NCEngine.addMessageHandler(
MESSAGE_HANDLER_ID,
object : MessageHandler {
override fun onMessageReceived(event: MessageReceivedEvent) {
val message = event.message
// Business filtering / analytics / risk control
}

override fun onMessageDeleted(event: MessageDeletedEvent) {
// Handle deleted messages
}
}
)

NCEngine.removeMessageHandler(MESSAGE_HANDLER_ID)

2. Sending-side interception (pre-send processing)

Before calling NCChatUI.sendMessage / NCChatUI.sendMediaMessage, process in your business layer:

  • Modify SendMessageParams / SendMediaMessageParams
  • Add metadata
  • Block sending if rules are triggered
kotlin
fun safeSend(identifier: ChannelIdentifier, params: SendMessageParams) {
// Business validation before sending
if (shouldBlock(params)) return

NCChatUI.sendMessage(identifier, params) { message, error ->
// Send result
}
}

3. Local message insertion interception

If you need business messages that are "inserted and displayed locally only", construct InsertMessageItem in your business layer first, then call NCChatUI.insertMessages(...).

kotlin
NCChatUI.insertMessages(identifier, listOf(item)) { result, error ->
// Local insertion result
}

Notes

  • Use the public APIs provided by NCChatUI / NCEngine / BaseChannel.
  • Use the message construction methods and callback patterns provided by the current SDK.