persistFcmToken abstract method
Persists the FCM token to the backend device record.
Called by NotificationService when it obtains/refreshes the token or clears it due to notifications being disabled. This method only handles backend persistence—NotificationService owns the token lifecycle (fetch/refresh/cache/local state).
When Called
- Initial token acquisition (first successful read after enabling)
- Token rotation/refresh (Firebase Messaging token refresh event)
- Token cleared due to local disablement (user disables notifications in-app while staying logged in)
Important: Logout Path
Do NOT call this method during logout. DeviceService deletes the device doc on logout via unregisterDevice; NotificationService should perform only local cleanup (clear cached token, detach listeners) without triggering a backend write that would race with device doc deletion.
Token Uniqueness
The backend enforces that a token appears on at most one device doc. On token update, it clears the same token from any other device docs (handles edge cases from offline failures or account switching).
Parameters
fcmToken: The new FCM token, ornullto clear the token.
Returns
Right(unit)on successLeft(RepositoryFailure)on failure
Example
// Token obtained
await deviceService.persistFcmToken(fcmToken: newToken);
// Token cleared (e.g., user revoked permission while logged in)
await deviceService.persistFcmToken(fcmToken: null);
Implementation
Future<Either<RepositoryFailure, Unit>> persistFcmToken({
required String? fcmToken,
});