fetchPreferences static method

Future<NotificationPreferences?> fetchPreferences({
  1. dynamic onSuccess(
    1. NotificationPreferences notificationPreferences
    )?,
  2. dynamic onError(
    1. CometChatException e
    )?,
})

Retrieves the current notification preferences for the logged-in user.

This method makes an asynchronous call to fetch the NotificationPreferences from CometChat servers. Upon successful retrieval, onSuccess is invoked with the NotificationPreferences object. In the case of an error, onError is called with a CometChatException.

Returns a Future<NotificationPreferences?> which completes with the fetched preferences, or null if an error occurs.

Implementation

static Future<NotificationPreferences?> fetchPreferences(
    {Function(NotificationPreferences notificationPreferences)? onSuccess,
    Function(CometChatException e)? onError}) async {
  try {
    final sdk = SdkRegistry.getInstance();
    final response = await sdk.notificationsApi.getPushPreferences();
    final data = response['data'] as Map<String, dynamic>;
    final notificationPreferencesObj = NotificationPreferences.fromMap(data);
    if (onSuccess != null) onSuccess(notificationPreferencesObj);
    return notificationPreferencesObj;
  } catch (e) {
    _handleError(e, onError);
  }
  return null;
}