feat(U4): background sync and local notifications for new mail #28

Merged
guettlibot merged 5 commits from task/u4-push-notifications into main 2026-05-14 02:06:36 +00:00
guettlibot commented 2026-05-13 22:51:15 +00:00 (Migrated from codeberg.org)

Implements background mail sync and local notifications so users are informed of new messages even when the app is backgrounded.

Changes

New packages

  • flutter_local_notifications ^18.0.1 — Android notification channel and permission handling
  • workmanager ^0.5.2 — Android WorkManager periodic background task

lib/core/services/notification_service.dart (new)

  • initNotifications() — initialises the plugin and requests POST_NOTIFICATIONS permission on Android 13+
  • showNewMailNotification(accountEmail) — posts a high-priority notification (top-level function, usable from background isolates)

lib/core/sync/background_sync.dart (new)

  • @pragma('vm:entry-point') callbackDispatcher() — WorkManager entry point in its own Dart isolate
  • registerBackgroundSync() — registers a 15-minute periodic task (requires network)
  • Background logic: opens a fresh AppDatabase + AccountRepositoryImpl, calls IMAP STATUS INBOX (UIDNEXT) per IMAP account, compares against the last stored value in syncStates table (resource type background_check), posts notification if UIDNEXT increased

lib/core/sync/account_sync_manager.dart

  • typedef OnNewMailCallback = Future<void> Function(String accountEmail)
  • Optional onNewMail parameter on AccountSyncManager and _AccountSync
  • In _AccountSync._idle(): tracks ImapMessagesExistEvent and fires the callback when newMessagesExists > oldMessagesExists

lib/di.dart — wires showNewMailNotification as the onNewMail callback

lib/main.dart — calls initNotifications() and (Android only) registerBackgroundSync() at startup

android/app/src/main/AndroidManifest.xml — adds POST_NOTIFICATIONS, RECEIVE_BOOT_COMPLETED, WAKE_LOCK permissions

android/app/build.gradle.kts — bumps minSdk to 23 (WorkManager requirement)

Implements background mail sync and local notifications so users are informed of new messages even when the app is backgrounded. ## Changes **New packages** - `flutter_local_notifications ^18.0.1` — Android notification channel and permission handling - `workmanager ^0.5.2` — Android WorkManager periodic background task **`lib/core/services/notification_service.dart`** (new) - `initNotifications()` — initialises the plugin and requests `POST_NOTIFICATIONS` permission on Android 13+ - `showNewMailNotification(accountEmail)` — posts a high-priority notification (top-level function, usable from background isolates) **`lib/core/sync/background_sync.dart`** (new) - `@pragma('vm:entry-point') callbackDispatcher()` — WorkManager entry point in its own Dart isolate - `registerBackgroundSync()` — registers a 15-minute periodic task (requires network) - Background logic: opens a fresh `AppDatabase` + `AccountRepositoryImpl`, calls IMAP `STATUS INBOX (UIDNEXT)` per IMAP account, compares against the last stored value in `syncStates` table (resource type `background_check`), posts notification if UIDNEXT increased **`lib/core/sync/account_sync_manager.dart`** - `typedef OnNewMailCallback = Future<void> Function(String accountEmail)` - Optional `onNewMail` parameter on `AccountSyncManager` and `_AccountSync` - In `_AccountSync._idle()`: tracks `ImapMessagesExistEvent` and fires the callback when `newMessagesExists > oldMessagesExists` **`lib/di.dart`** — wires `showNewMailNotification` as the `onNewMail` callback **`lib/main.dart`** — calls `initNotifications()` and (Android only) `registerBackgroundSync()` at startup **`android/app/src/main/AndroidManifest.xml`** — adds `POST_NOTIFICATIONS`, `RECEIVE_BOOT_COMPLETED`, `WAKE_LOCK` permissions **`android/app/build.gradle.kts`** — bumps `minSdk` to 23 (WorkManager requirement)
Sign in to join this conversation.