disableNotifications method

Future<void> disableNotifications()

Disables notifications at the app level.

This is a preference-level toggle (not OS permission). It:

  1. Attempts to unregister the FCM token on the backend (best-effort)
  2. Stops the token refresh listener
  3. Deletes the FCM token from Firebase
  4. Clears cached tokens
  5. Sets the app-level notifications enabled flag to false

If the user re-enables via enableNotifications, there is no extra system prompt unless they revoked permission in device settings.

Example:

// In your settings screen
await notificationService.disableNotifications();
setState(() => notificationsEnabled = false);

Implementation

Future<void> disableNotifications() async {
  logd('Disabling notifications at app level');

  // Best-effort backend unregister
  if (_onTokenChanged != null && _cachedFcmToken != null) {
    try {
      await _onTokenChanged!(null, _cachedFcmToken);
      logd('Unregistered FCM token on backend');
    } catch (e) {
      logw('Failed to unregister FCM token (continuing with disable): $e');
    }
  }

  // Stop token refresh listener
  await _tokenRefreshSubscription?.cancel();
  _tokenRefreshSubscription = null;

  // Delete FCM token from Firebase
  try {
    await FirebaseMessaging.instance.deleteToken();
  } catch (e) {
    logw('Failed to delete FCM token: $e');
  }

  // Clear cached tokens
  await clearFcmToken();

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

  logi('Notifications disabled at app level');
}