Skip to content

fix(alerts): make background notifications work with the app closed - #9

Merged
JuggyBob merged 2 commits into
mainfrom
fix/background-notifications
Nov 8, 2026
Merged

JuggyBob merged 2 commits into
mainfrom
fix/background-notifications

Conversation

@JuggyBob

@JuggyBob JuggyBob commented Nov 8, 2026

Copy link
Copy Markdown
Owner

Deal alerts could fail silently in four separate ways. All four are reproduced, fixed, and verified on a device.

The four defects

  • A fresh install was never asked for the notification permission. Deal alerts default to on, but the only POST_NOTIFICATIONS request lived in the switch's off-to-on handler. On Android 13+ the permission starts denied, so the check ran, the watch state advanced, and every notification was dropped.
  • Every process start pushed the alarm an hour into the future. AppContainer.start() re-armed it with FLAG_UPDATE_CURRENT, including the process WorkManager creates for its own periodic worker. On a device woken often enough the punctual alarm never fired.
  • A change detected while notifications were blocked was recorded as delivered. Granting the permission later brought nothing back.
  • Free games were skipped when the wishlist was empty, even though they are announced without being saved.

What changed

  • One-time permission explanation after the intro (NotificationPermissionPrompt), a remembered answer, and an Allow notifications action in Settings.
  • AlarmSchedule.shouldArm: persist the due time, use PendingIntent.FLAG_NO_CREATE, and never move an armed alarm. Only a firing, reboot, app replacement, or explicit on/off change re-arms.
  • AlertDeliveryPolicy.shouldAdvanceState: do not consume an announcement nobody saw. The worker also checks canPost() before fetching.
  • Free-game checks run with an empty wishlist.
  • Settings shows the last check, its outcome, the next check, a Check now button, and a battery-optimisation hint.
  • BootReceiver respects the setting and bounds its DataStore read.
  • "Delete all app data" clears the alert records and restarts the default schedule.

Verification

431 unit tests across 35 suites, 0 failures; new tests for the alarm rule, the delivery rule, the status record and the permission policy. Lint clean; debug and release build.

On the emulator (Android 16):

  • Fresh install: prompt appears, granted=true, no repeat after relaunch.
  • dumpsys alarm: origWhen unchanged across a process kill and relaunch.
  • App process killed: WorkManager ran a check and posted two free-game notifications.
  • Permission revoked: status NOTIFICATIONS_BLOCKED, watch state untouched; after granting, the same two games are announced.
  • Alarm fired while the app was on the home screen and re-armed exactly 60 minutes later.
  • The release variant's worker completed a check under R8.

Version

versionCode 10, versionName 1.1.2-release.

Deal alerts could fail silently in four separate ways. All four are
reproduced, fixed, and verified on a device.

## The four defects

**A fresh install was never asked for the notification permission.** Deal
alerts default to on, but the only POST_NOTIFICATIONS request lived in the
switch's off-to-on handler, which a fresh install never reaches. On Android 13
and above the permission starts denied, so the check ran, the watch state
advanced, and every notification was dropped. The app now explains why it
needs the permission after the intro and asks once; "Not now" is remembered,
and Settings keeps an Allow notifications action for a permanent denial.

**Every process start pushed the alarm an hour into the future.**
AppContainer.start() called scheduleAlarm(), which used
setExactAndAllowWhileIdle(now + 60 min) with FLAG_UPDATE_CURRENT on every
process creation - including the process WorkManager creates for its own
periodic worker. On a device woken often enough, the punctual alarm never
fired, leaving only the deferrable periodic worker. That is the "no
notification when the app is closed" report, especially on battery-managed
phones.

The due time is now persisted, PendingIntent.FLAG_NO_CREATE tells the
scheduler whether the alarm still exists, and an armed alarm is left alone.
Only a firing, a reboot, an app replacement, or an explicit on/off change
re-arms it. AlarmSchedule.shouldArm is pure and tested.

**A change detected while notifications were blocked was recorded as
delivered.** The worker saved detection.nextState unconditionally. If
announce() posted nothing, the sale was still marked announced, so granting
the permission later brought nothing back. The worker now checks canPost()
before fetching, and AlertDeliveryPolicy.shouldAdvanceState advances the
record only when there was nothing to announce, something was posted, or the
first-run baseline is being established.

**Free games were skipped when the wishlist was empty.** SaleDetector
announces free games without a wishlist, but the worker returned early with
"nothing to watch for". The early return is gone.

## Also fixed

- "Delete all app data" now clears the watch state and the status record,
  resets the in-memory status, and restarts the default schedule.
- BootReceiver respects the setting instead of re-arming the alarm after a
  reboot even when the user had turned alerts off.
- BootReceiver bounds its DataStore read so a boot broadcast cannot hang.

## Added

- Settings shows the last check and what it did, the next scheduled check, and
  a Check now button. AlertStatusStore persists the record and publishes it
  through a process-wide StateFlow so the screen updates live.
- A battery-optimisation hint with a link to the system screen. Several
  manufacturers defer even exact alarms until the app is opened; no app-side
  code can override that, but the user can only fix it if they are told.

## Verification

431 unit tests across 35 suites, 0 failures. New suites: AlarmScheduleTest,
AlertDeliveryPolicyTest, AlertStatusStoreTest, NotificationPermissionPolicyTest.
Lint clean; debug and release build.

On the emulator (Android 16):
- Fresh install: the permission prompt appears, granted=true, and it does not
  reappear after a relaunch.
- dumpsys alarm: origWhen is byte-for-byte unchanged across a process kill and
  relaunch; the log says "already armed".
- App process killed: WorkManager ran a check and posted two free-game
  notifications.
- Permission revoked: status NOTIFICATIONS_BLOCKED and the watch state is
  untouched; after granting it, the same two games are announced.
- The alarm fired while the app was on the home screen and re-armed exactly
  60 minutes later.
- The release variant's worker completed a check under R8.

## Version

versionCode 10, versionName 1.1.2-release.
`enqueueNow()` enqueues a non-unique one-time request under the ONE_TIME_WORK
tag, but `stop()` cancelled it with `cancelUniqueWork(ONE_TIME_WORK)`, which
matches nothing. A check queued just before the user turned deal alerts off
could therefore still run; the worker would see the setting off and skip it, so
no notification was posted, but the cancellation was not doing what its
comment claimed. It now cancels by tag.

Also records the four 1.1.2 defects in docs/ENGINEERING.md, which mirrors the
DEV_NOTES history for readers who only open the docs folder.
@JuggyBob
JuggyBob merged commit 14dd87e into main Nov 8, 2026
3 checks passed
@JuggyBob
JuggyBob deleted the fix/background-notifications branch November 8, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant