Implement ApprovalNotificationService for FCM and Realtime delivery
epic-expense-approval-workflow-foundation-task-009 — Implement ApprovalNotificationService that delivers approval status change notifications via both FCM (for background/offline) and Supabase Realtime (for foreground). Expose notifyClaimApproved(), notifyClaimRejected(), and notifyClaimPendingReview() methods. Integrate with FCM token manager and handle token refresh. Ensure coordinator notifications include claim summary, claimant name, and amount.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
Structure the service with a dual-delivery strategy: check if RealtimeApprovalSubscription.isConnected before deciding delivery channel. For coordinator notifications, query assignment table for all coordinators in the org and batch their FCM tokens — use a single Edge Function call with a token array rather than N individual calls. The Edge Function should accept { tokens: string[], payload: FCMPayload } to support batch dispatch. FCM notification tap handling: use FirebaseMessaging.onMessageOpenedApp stream to navigate to the specific claim detail screen using the claimId in the notification data.
Place token refresh logic in a dedicated DeviceTokenSyncService called from ApprovalNotificationService to keep responsibilities separate.
Testing Requirements
Unit tests using flutter_test with mocked Supabase client and mocked FCM edge function HTTP client. Test cases: (1) notifyClaimApproved invokes edge function with correct payload schema, (2) notifyClaimPendingReview sends to all coordinator tokens in org, (3) stale token (FCM 404 response) triggers token deletion from device_tokens, (4) foreground detection routes notification to Realtime stream not FCM, (5) Edge Function unavailability surfaces as ApprovalNotificationError with retry suggestion. Mock the Edge Function HTTP call using http package mock — do not call real FCM in unit tests.
Optimistic locking in ExpenseClaimStatusRepository may produce excessive concurrency exceptions in high-volume coordinator sessions where multiple coordinators process the same queue simultaneously, causing confusing UI errors and coordinator frustration.
Mitigation & Contingency
Mitigation: Design the locking strategy with a short retry window (1-2 automatic retries with 200ms back-off) before surfacing the error to the UI. Document the concurrency model clearly so the UI layer can display a contextual 'claim was already actioned' message rather than a generic error.
Contingency: If contention remains high under load testing, switch to a last-writer-wins update with a conflict notification rather than a hard block, and log all concurrent edits for audit purposes.
FCM device tokens stored for peer mentors may be stale (app reinstalled, token rotated) causing push notifications for claim status changes to silently fail, leaving submitters unaware their claim was approved or rejected.
Mitigation & Contingency
Mitigation: Implement token refresh on every app launch and store updated tokens in Supabase. ApprovalNotificationService should fall back to in-app Realtime delivery when FCM returns an invalid-token error and should queue a token refresh request.
Contingency: If FCM delivery rates fall below acceptable thresholds in production monitoring, add a polling fallback in the peer mentor claim list screen that checks status on foreground resume.
Supabase Realtime has per-project channel and connection limits. If many coordinators and peer mentors are simultaneously subscribed across multiple screens, the project may hit quota limits causing subscription failures.
Mitigation & Contingency
Mitigation: Design RealtimeApprovalSubscription to use a single shared channel per user session rather than per-screen subscriptions. Implement subscription reference counting so channels are only opened once and reused across screens.
Contingency: Upgrade the Supabase plan tier if limits are reached, and implement graceful degradation to polling with a 30-second interval when Realtime is unavailable.