showNotification method

Future<NotificationFailure?> showNotification(
  1. NotificationPayload payload, {
  2. bool respectUserPreferences = true,
})

Shows a notification immediately with user preferences applied. Uses local repository for immediate display.

Implementation

Future<NotificationFailure?> showNotification(
  NotificationPayload payload, {
  bool respectUserPreferences = true,
}) async {
  try {
    if (!_isInitialized) {
      return NotificationFailure.initialization(
        details: 'Notification manager not initialized',
      );
    }

    // Apply user preferences if enabled
    NotificationPayload finalPayload = payload;
    final UserPreferencesModel? userPreferences = _userPreferences;
    if (respectUserPreferences && userPreferences != null) {
      final NotificationPayload originalPayload = payload;
      finalPayload = _applyUserPreferences(payload, userPreferences);

      // Log only significant changes
      if (finalPayload.silent) {
        safeDebugLog(
          '''πŸ”‡ Notification "${payload.title}" set to SILENT due to user preferences''',
        );
      }
      if (originalPayload.enableVibration && !finalPayload.enableVibration) {
        safeDebugLog(
          '''πŸ“³ Vibration disabled for "${payload.title}" by user preferences''',
        );
      }
      if (originalPayload.playSound && !finalPayload.playSound) {
        safeDebugLog(
          '''πŸ”Š Sound disabled for "${payload.title}" by user preferences''',
        );
      }
    }

    // Check if notifications are globally disabled
    if (respectUserPreferences &&
        _userPreferences?.globalNotificationsEnabled == false) {
      safeDebugLog(
        '''🚫 SCHEDULED NOTIFICATION BLOCKED: "${payload.title}" - Notifications globally disabled''',
      );
      return NotificationFailure.configuration(
        details: 'Notifications disabled by user preferences',
      );
    }

    final NotificationFailure? result = await _localRepository
        .showNotification(finalPayload);

    if (result != null) {
      safeDebugLog(
        '❌ Failed to show notification "${payload.title}": ${result.message}',
      );
    }

    return result;
  } catch (e, stackTrace) {
    safeDebugLog('πŸ’₯ Exception showing notification "${payload.title}": $e');
    return NotificationFailure.delivery(
      details: e.toString(),
      stackTrace: stackTrace,
    );
  }
}