Handle unread count
The Nexconn SDK provides methods to get and clear unread message counts.
Get the unread count for a channel
Access the unreadCount property on the channel object to get the current unread message count.
- Kotlin
- Java
kotlin
val channel = DirectChannel("userId")
val count = channel.unreadCount
Log.d("Unread", "Unread count: $count")
Java
DirectChannel channel = new DirectChannel("userId");
int count = channel.getUnreadCount();
Log.d("Unread", "Unread count: " + count);
Clear the unread count
Use clearUnreadCount() to reset the unread count for a channel.
- Kotlin
- Java
kotlin
val channel = DirectChannel("userId")
channel.clearUnreadCount { success, error ->
if (error == null && success == true) {
Log.d("Unread", "Unread count cleared")
}
}
Java
DirectChannel channel = new DirectChannel("userId");
channel.clearUnreadCount(new OperationHandler<Boolean>() {
@Override
public void onResult(Boolean success, NCError error) {
if (error == null && success) {
Log.d("Unread", "Unread count cleared");
}
}
});
Get the first unread message
Use getFirstUnreadMessage() to retrieve the earliest unread message for navigation purposes.
- Kotlin
- Java
kotlin
val channel = DirectChannel("userId")
channel.getFirstUnreadMessage { message, error ->
if (error == null && message != null) {
Log.d("Unread", "First unread: ${message.messageId}")
} else if (message == null) {
Log.d("Unread", "No unread messages")
}
}
Java
DirectChannel channel = new DirectChannel("userId");
channel.getFirstUnreadMessage(new OperationHandler<Message>() {
@Override
public void onResult(Message message, NCError error) {
if (error == null && message != null) {
Log.d("Unread", "First unread: " + message.getMessageId());
}
}
});
Get the total unread count across channel types
Use the static BaseChannel.getTotalUnreadCount() method to get the combined unread count for all supported channel types.
- Kotlin
- Java
kotlin
BaseChannel.getTotalUnreadCount { count, error ->
if (error == null) {
Log.d("Unread", "Total unread: $count")
}
}
Java
BaseChannel.getTotalUnreadCount(new OperationHandler<Integer>() {
@Override
public void onResult(Integer count, NCError error) {
if (error == null) {
Log.d("Unread", "Total unread: " + count);
}
}
});
Parameters
unreadCount (property)
| Property | Type | Description |
|---|---|---|
unreadCount | Int | Unread message count for the current channel. |
clearUnreadCount
| Parameter | Type | Description |
|---|---|---|
handler | OperationHandler<Boolean> | The completion callback. |
getFirstUnreadMessage
| Parameter | Type | Description |
|---|---|---|
handler | OperationHandler<Message> | Callback returning the first unread message, or null if there are no unread messages. |
getTotalUnreadCount (static method)
| Parameter | Type | Description |
|---|---|---|
handler | OperationHandler<Int> | Callback returning the total unread count across all supported channel types. |
Important notes
- The unread count only reflects messages in the local database.
- Clearing the unread count marks messages as read but does not delete them.
- Clear the unread count when the user views messages.
- Open channels do not support unread counts.