Skip to main content

Receive messages

Listen for incoming messages from the SDK and handle them in your application logic.

Listen for incoming messages

Register a message event handler using addMessageHandler to receive messages. Register the listener early in your application lifecycle.

kotlin
NCEngine.addMessageHandler("MESSAGE_HANDLER_ID", object : MessageHandler {
override fun onMessageReceived(event: MessageReceivedEvent) {
Log.d("Message", "Received: ${event.message.content}")
updateMessageUI(event.message)
playNotificationSound()
}

override fun onMessageDeleted(event: MessageDeletedEvent) {
event.messages.forEach { removeMessageFromUI(it) }
}

override fun onMessagesModified(event: MessagesModifiedEvent) {
event.messages.forEach { updateMessageInUI(it) }
}
})

Remove the listener

Remove the handler when it is no longer needed to prevent memory leaks:

kotlin
NCEngine.removeMessageHandler("MESSAGE_HANDLER_ID")

Message received status

The Message class includes a receivedStatusInfo: MessageReceivedStatusInfo object with the following status flags:

StatusDescription
isReadWhether the message has been read on the current device or any other device.
isListenedWhether a voice message has been listened to. Applies only to voice messages.
isDownloadedWhether the media file has been downloaded. Applies only to media messages.
isRetrievedWhether another device (online or previously logged in) has already received this message.

Disable message deduplication

The deduplication mechanism automatically removes duplicate messages when receiving direct channel, group channel, system, and open channel messages. If your app stores a large volume of local messages, deduplication may cause performance issues. In that case, you can disable it.

Why duplicate messages occur

Duplicates can occur under weak network conditions. When User A sends a message, it reaches the server and is delivered to User B. However, A may not receive the server acknowledgment due to network issues, causing A to resend. User B then receives a duplicate (same content, different UID).

Disable deduplication

note

Message deduplication configuration is not currently available in the Nexconn SDK. Contact support for guidance on this advanced configuration.