Implement iOS notification permission request flow
epic-push-notification-delivery-foundation-task-009 — In NotificationPermissionManager, implement requestPermission() for iOS using firebase_messaging's requestPermission() with alert, badge, and sound set to true. Check current authorization status via getNotificationSettings() before requesting. Return a PermissionResult enum (granted, denied, provisional, notDetermined). Store the result in local preferences and trigger FCM token registration if granted.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Define PermissionResult as a Dart enum in lib/notifications/permission_result.dart — do not reuse any third-party enum to keep the domain model clean. In NotificationPermissionManager (lib/notifications/notification_permission_manager.dart), inject FirebaseMessaging and SharedPreferences via constructor for testability. The mapping from AuthorizationStatus to PermissionResult should be a pure switch expression with an exhaustive default arm. On iOS, 'provisional' authorization (quiet notifications) is a valid state — treat it as granted for token registration purposes but store it distinctly so the UI can optionally prompt the user to upgrade to full authorization later.
Do not call requestPermission() unless the pre-permission rationale dialog (task-011) has already been shown.
Testing Requirements
Unit tests (flutter_test + mockito): mock FirebaseMessaging.getNotificationSettings() returning each of the four AuthorizationStatus values; verify correct PermissionResult mapping; verify SharedPreferences write with correct key/value; verify registerTokenOnSignIn is called only when result is 'granted'. Widget test: render a test widget that calls requestPermission() and assert the returned enum value. Manual QA: fresh iOS simulator install → confirm dialog appears; second launch → confirm no dialog; Settings → disable notifications → reopen app → confirm 'denied' result stored and no crash.
iOS only allows one system permission prompt per app install. If the rationale dialog timing or content is wrong the user may permanently deny permissions during onboarding, permanently blocking push delivery for that device with no recovery path short of manual system settings navigation.
Mitigation & Contingency
Mitigation: Design and user-test the rationale dialog content and trigger point (after onboarding value-demonstration step, not at first launch). Implement the settings-deep-link fallback in NotificationPermissionManager so the permission state screen always offers a path to system settings if denied.
Contingency: If denial rates are high in TestFlight testing, revise the rationale copy and trigger timing before production release. Ensure the in-app notification centre provides full value without push so denied users are not blocked from the feature.
FCM token rotation callbacks can fire at any time, including during app termination or network outage. If the token rotation is not persisted reliably the backend trigger service will dispatch to a stale token, resulting in silent notification failures that are hard to diagnose.
Mitigation & Contingency
Mitigation: Persist token rotation updates with a local queue that retries on next app foreground if network is unavailable. Use Supabase upsert by (user_id, device_id) to prevent duplicate token rows and ensure the latest token always wins.
Contingency: If token staleness is observed in production, add a token validity check on each app foreground and force a re-registration if the stored token does not match the FCM-reported current token.
Incorrect RLS policies on notification_preferences or fcm_tokens could expose one user's preferences or device tokens to another user, or could block the backend Edge Function service role from reading token lists needed for dispatch, silently dropping all notifications.
Mitigation & Contingency
Mitigation: Write explicit RLS policy tests using the Supabase test harness covering user-scoped read/write, service-role read for dispatch, and cross-user access denial. Review policies during code review with a security checklist.
Contingency: Maintain a rollback migration that reverts the RLS changes, and add an integration test in CI that asserts the service role can query all tokens and that a normal user JWT cannot access another user's token rows.