fix(android): visible backup notification on API 31+ background runs #1339
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/android-notification"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
On Android 12+ (API 31+) a periodic backup run starts from true background, where WorkManager's
setForeground(which callsstartForegroundService) throwsForegroundServiceStartNotAllowedException.BackupWorkerswallowed that into the trace log and the run continued with no foreground service and no notification. The manifest deliberately had noPOST_NOTIFICATIONSflow.Changes (
apps/android/apponly —:coreuntouched)sync/BackupNotifications.kt(new) — single owner of the notification surface: channel setup, the shared ongoing "backup running" builder,NotificationManagerfallback (postFallback/cancelFallback), and the API 31+ foreground-promotion gate (foregroundPromotionEligible) reading this process's importance viagetRunningAppProcesses()(public API; the dedicatedgetMyMemoryStategetter is hidden from app code).sync/BackupWorker.kt— when the gate says promotion is not allowed (periodic background runs): skip the doomedsetForeground, trace once, post the ongoing notification directly; when promotion fails from an eligible state: same fallback. The fallback is cancelled in afinallyonly if this path posted it (a promoted FGS notification is torn down by the system). All existingBackupRunTracebreadcrumbs kept.AndroidManifest.xml— addsPOST_NOTIFICATIONS; stale "No POST_NOTIFICATIONS flow" comment rewritten;FOREGROUND_SERVICE_TYPE_DATA_SYNCwiring untouched.ui/backup/BackupScreen.kt— third step of the existing ask-once permission chain: one-time short rationale dialog (what the notification shows; swiping it away doesn't stop the backup), then the system prompt on API 33+ only. Declining never gates backup. No new design system — reuses the screen'sAlertDialogidiom. Strings in de (default), en, da.BackupNotificationsTest: 11 Robolectric tests (channel idempotency + low importance, ongoing flag, fallback post→cancel, below-O behavior, promotion gate across importances/SDKs). Shadow APIs verified against the exact Robolectric 4.17 jar before use.KNOWN_ISSUES.md— appended ISSUE-176 (med/mitigated, area:android) documenting the fix and the accepted limitation that the fallback carries no FGS process protections.Notable fix found on the way
The old builder used the API 26+ two-arg
Notification.Builder(context, channel)unconditionally, so on the Android 7.1 device (minSdk 25) every notification build threwNoSuchMethodError, silently swallowed by the promotionrunCatching— no notification ever appeared there. The consolidated builder is now channel-less below O.Verification
Merged manifest confirmed to contain
android.permission.POST_NOTIFICATIONSafter the merge.Scope note for reviewers: a
local.properties(SDK path) was created locally to build; it is gitignored and not part of this PR.90598eb7e4bc4fb4a78a