enableNotifications method

Future<NotificationInitResult> enableNotifications()

Enables notifications at the app level.

This is a thin wrapper that:

  1. Sets the app-level notifications enabled flag to true
  2. Re-checks permission status
  3. Requests permission if needed (may show system prompt)
  4. Fetches a fresh FCM token and syncs to backend
  5. Restarts the token refresh listener

Returns a NotificationInitResult indicating the outcome.

Example:

// In your settings screen
final result = await notificationService.enableNotifications();
if (result == NotificationInitResult.success) {
  setState(() => notificationsEnabled = true);
}

Implementation

Future<NotificationInitResult> enableNotifications() async {
  logd('Enabling notifications at app level');

  // Set app-level flag first
  final prefs = await SharedPreferences.getInstance();
  await prefs.setBool(_keyNotificationsEnabled, true);

  // Reset initialization state so initializeNotifications can run
  _hasFcmTokenInitialized = false;

  // Use the existing init flow
  final result = await initializeNotifications();

  if (result == NotificationInitResult.success ||
      result == NotificationInitResult.alreadyInitialized) {
    logi('Notifications enabled at app level');
  } else {
    logw('Failed to enable notifications: $result');
    // Revert the flag if we couldn't enable
    await prefs.setBool(_keyNotificationsEnabled, false);
  }

  return result;
}