feat: implement full FCM token cleanup on logout by deleting both remote and local tokens

This commit is contained in:
pradeepkumar
2026-04-13 16:51:26 +05:30
parent de84a2b883
commit 583d9f7428
2 changed files with 23 additions and 10 deletions

View File

@@ -254,16 +254,26 @@ class PushNotificationService {
_dispatchTap(Map<String, dynamic>.from(message.data));
}
/// Unregister FCM token from backend (call on logout).
/// Unregister FCM token from backend and delete locally (call on logout).
Future<void> unregister() async {
if (_currentToken == null) return;
try {
await _repo.removeFcmToken(_currentToken!);
debugPrint('[FCM] Token unregistered from backend');
} catch (e) {
debugPrint('[FCM] Failed to unregister token: $e');
// Remove from backend so it stops sending push to this device
if (_currentToken != null) {
try {
await _repo.removeFcmToken(_currentToken!);
debugPrint('[FCM] Token unregistered from backend');
} catch (e) {
debugPrint('[FCM] Failed to unregister token: $e');
}
}
_currentToken = null;
// Delete the local FCM token so Firebase stops delivering notifications
try {
await _messaging?.deleteToken();
debugPrint('[FCM] Local FCM token deleted');
} catch (e) {
debugPrint('[FCM] Failed to delete local token: $e');
}
}
/// Re-register token after login.

View File

@@ -78,6 +78,8 @@ class AuthNotifier extends StateNotifier<AuthState> {
// when token refresh fails
ApiClient.onForceLogout = () {
SocketService().disconnect();
// Delete local FCM token so push notifications stop
PushNotificationService().unregister();
// Only reset if still authenticated — avoid showing errors on login screen
if (state.status == AuthStatus.authenticated) {
state = const AuthState();
@@ -121,12 +123,13 @@ class AuthNotifier extends StateNotifier<AuthState> {
state = const AuthState();
// Disconnect socket immediately
SocketService().disconnect();
// Clear tokens BEFORE any API calls to prevent 401 errors during logout
await SecureStorage.clearTokens();
// Best-effort cleanup — tokens already cleared, ignore any errors
// Unregister FCM BEFORE clearing tokens — backend call needs auth header,
// and deleteToken() stops Firebase from delivering to this device
try {
await PushNotificationService().unregister();
} catch (_) {}
// Logout API call + clear local tokens
await _repository.logout();
}
Future<void> login({