getUnreadMessageCount static method

Future<Map<String, Map<String, int>>?> getUnreadMessageCount({
  1. bool? hideMessagesFromBlockedUsers,
  2. dynamic onSuccess(
    1. Map<String, Map<String, int>> message
    )?,
  3. dynamic onError(
    1. CometChatException excep
    )?,
})

Gives count of unread messages for every one-on-one and group conversation.

on Success will return 2 keys:

  1. user - The value for this key holds another map that holds the UIDs of users and their corresponding unread message counts.
  2. group - The value for this key holds another map that holds the GUIDs of groups and their corresponding unread message counts.

Migration Note: Migrated from platform channels to native Dart implementation. Uses MessageRepository for unread count retrieval. Behavior and signature remain identical for backward compatibility.

Android Reference: CometChat.getUnreadMessageCount(boolean hideMessagesFromBlockedUsers)

method could throw PlatformException with error codes specifying the cause

Implementation

static Future<Map<String, Map<String, int>>?> getUnreadMessageCount(
    {bool? hideMessagesFromBlockedUsers,
    Function(Map<String, Map<String, int>> message)? onSuccess,
    Function(CometChatException excep)? onError}) async {
  try {
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Call native Dart message repository
    final count = await sdk.messages.getUnreadMessageCount(
      hideMessagesFromBlockedUsers: hideMessagesFromBlockedUsers ?? false,
    );

    // Ensure backward compatibility - add empty maps if keys don't exist
    if (!count.containsKey('group')) count['group'] = {};
    if (!count.containsKey('user')) count['user'] = {};

    // Call success callback
    if (onSuccess != null) onSuccess(count);
    return count;
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    _errorCallbackHandler(cometChatEx, null, null, onError);
    return null;
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
    return null;
  }
}