Repository navigation
Fetch the Expo push token once per device token on mobile - #8225
Merged
Merged
Conversation
Each app resume ran two push syncs (AppState active and the realtime reconnect), and each sync called getExpoPushTokenAsync, a POST to exp.host, before it found the registration up to date. The Expo push token only changes with the device token, and expo-notifications already sends device token changes to Expo itself. Keep the Expo push token in the registration controller for the app process. Drop it when the device token rolls or when Android releases the device token, so the next sync fetches it again.
3 tasks done
ymichael
added a commit
that referenced
this pull request
Dec 1, 2026
## Human comments ## What was wrong `syncPushRegistration` awaited the Expo push token (`getExpoPushTokenAsync`: the APNs device token, then a POST to exp.host) with no time limit. When either step never settled, the sync never returned. The controller keeps `syncing: true` until a sync returns, and all syncs run one after another on one shared queue. So the switch spun forever, the phone never sent `pushSubscriptions.add`, and syncs for other servers waited behind the stuck one (#8392). #8225 (already merged) doesn't change this: it still fetches the token once at cold start, and that fetch has no time limit. ## What changed - `apps/mobile/src/data/notifications/push-registration.ts`: the token step gives up after `PUSH_TOKEN_TIMEOUT_MS` (20 s) and returns `{ action: "failed", step: "token" }`. The status reads "Could not register: No push token from Apple or Expo after 20 s", the existing toggle toast shows the error, the switch becomes usable again, and queued syncs run. A later attempt fetches again; #8225's cached token stays empty after a failure. On iOS, a new native `getDevicePushTokenAsync` call replaces the stuck one, so a retry doesn't conflict with the stuck request. - `push-registration-controller.ts`: turning the switch on starts a second sync of the same server (`PushNotificationsHost`'s effect reruns when `activeEnabled` changes), and the two are coalesced into one sync plus a rerun. Without this change the rerun fetched the token again, so the spinner lasted about 40 s and the toast showed late. A rerun inside the same coalesced sync now reuses the token failure instead of waiting again. A rerun that unregisters needs no token and runs normally. - No server, wire, CLI or protocol changes. ## How you verified - New test `gives up on a push token request that never settles, once per coalesced sync, and runs the next sync`. Without the timeout it never finishes (vitest timeout at 15 s). Without the rerun change it fails (`expected { action: 'registered' } to deeply equal { action: 'failed', … }`). - `pnpm exec turbo run typecheck test lint lint:type-aware --continue --filter=@bb/mobile`: 8 tasks successful, 365 tests passed, 0 lint errors. - iOS 27.2 Simulator, local Debug dev-client build against a `start:worktree` server: - Before: with a temporary QA shim that holds every exp.host `fetch` open, the switch stayed on "Registering…" with a spinner for more than 3 minutes, and `pushSubscriptions.list` on the server was empty. - After: on a cold start and on toggling off and on, the spinner stopped at about 20 s with "Could not register: No push token from Apple or Expo after 20 s" and the toast "Could not turn on notifications". - Apple-side hang, without the shim: right after a simulator boot, the token request hung before any exp.host request. CFNetwork logged no exp.host connection, and apsd never logged the app. With the fix it failed after 20 s. Once apsd was up, toggling registered and the server listed the subscription ("On · registered with this server"). So the hang on vv2002's phone may be in Apple's device-token step rather than exp.host; the timeout covers both. Fixes #8392 > AGENT GENERATED 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
scowalt
pushed a commit
to scowalt/bb
that referenced
this pull request
Dec 1, 2026
## Human comments ## What was wrong Each app resume runs two push syncs: the AppState `active` handler (it also reads the OS permission, get-bb#2881) and the effect that runs again when realtime reconnects (`PushNotificationsHost.tsx`). Each sync called `syncPushRegistration`, which calls `getExpoPushTokenAsync` before it checks the stored record. That call is always a POST to `https://exp.host/--/api/v2/push/getExpoPushToken`. The Expo push token changes only with the device token, and expo-notifications 58.1.8 already sends device token changes to Expo by itself (`DevicePushTokenAutoRegistration.fx.ts`, turned on by the first `getExpoPushTokenAsync`). So the phone made 2 exp.host round trips per resume to learn a token it already had. I confirmed the call pattern with the real `createPushRegistrationController` and counting fakes before changing anything (numbers below). ## What changed - `apps/mobile/src/data/notifications/push-registration-controller.ts`: the controller keeps the Expo push token for the app process and gives `syncPushRegistration` that value instead of a new fetch. It drops the token when the device token rolls (`handleTokenRolled`, after its existing duplicate check) and when Android releases the device token (`releaseUnusedDeviceToken`), so the next sync fetches again. Cold start still fetches once and compares the token with the stored record, as before. The 24 h re-registration still runs; it re-registers with the kept token. - `push-registration-controller.test.ts`: a new test for the resume pattern and a token roll. Two tests had the old fetch counts. The permission-revoke test replaced `notifications.getPermission` after the controller was built; it now uses a permission variable in `setup()` and also asserts that the profile is unregistered. - No wire, CLI, server or protocol changes. Deviation from the issue's idea (check the record age before the fetch, and set `registeredAt: 0` on a token roll): that also gives 0 fetches per resume, but it stops the token check at cold start for up to 24 h, writes the persisted record on every roll, and does not cover the Android path where the controller releases the device token while other profiles keep a fresh record. The in-memory token has none of these gaps. If a token event arrives while a fetch is in flight, the in-flight result is kept; the Expo push token does not depend on the device token, so this can only keep the same value. ## How you verified Before/after with the real controller and counting fakes (script kept out of the commit): | Scenario | exp.host token fetches before | after | server registers before | after | | --- | --- | --- | --- | --- | | Cold start | 1 | 1 | 1 | 1 | | 10 resumes (AppState active + reconnect effect) | 20 | 0 | 0 | 0 | | Device token roll, then one sync | 2 | 1 | 0 | 0 | New test `fetches the Expo push token once per device token, not on every resume sync`, run against the unfixed controller: ``` FAIL coalesces concurrent syncs for one profile into one in-flight run plus a trailing run FAIL fetches the Expo push token once per device token, not on every resume sync AssertionError: expected "vi.fn()" to be called once, but got 2 times AssertionError: expected "vi.fn()" to be called once, but got 4 times Tests 2 failed | 7 passed (9) ``` With the fix: 9 passed. Each token reset is covered: without the reset in `releaseUnusedDeviceToken`, `waits for token deletion before registering a newly enabled server` fails; without the reset in `handleTokenRolled`, the new test and `ignores repeated device token events and events caused by an active sync` fail. `pnpm exec turbo run typecheck test lint lint:type-aware --continue --filter=@bb/mobile`: 8 tasks successful, 49 test files and 360 tests passed, lint 0 errors (6 existing warnings in other files), lint:type-aware 0 warnings. Fixes get-bb#8224 > AGENT GENERATED
scowalt
pushed a commit
to scowalt/bb
that referenced
this pull request
Dec 1, 2026
## Human comments ## What was wrong `syncPushRegistration` awaited the Expo push token (`getExpoPushTokenAsync`: the APNs device token, then a POST to exp.host) with no time limit. When either step never settled, the sync never returned. The controller keeps `syncing: true` until a sync returns, and all syncs run one after another on one shared queue. So the switch spun forever, the phone never sent `pushSubscriptions.add`, and syncs for other servers waited behind the stuck one (get-bb#8392). get-bb#8225 (already merged) doesn't change this: it still fetches the token once at cold start, and that fetch has no time limit. ## What changed - `apps/mobile/src/data/notifications/push-registration.ts`: the token step gives up after `PUSH_TOKEN_TIMEOUT_MS` (20 s) and returns `{ action: "failed", step: "token" }`. The status reads "Could not register: No push token from Apple or Expo after 20 s", the existing toggle toast shows the error, the switch becomes usable again, and queued syncs run. A later attempt fetches again; get-bb#8225's cached token stays empty after a failure. On iOS, a new native `getDevicePushTokenAsync` call replaces the stuck one, so a retry doesn't conflict with the stuck request. - `push-registration-controller.ts`: turning the switch on starts a second sync of the same server (`PushNotificationsHost`'s effect reruns when `activeEnabled` changes), and the two are coalesced into one sync plus a rerun. Without this change the rerun fetched the token again, so the spinner lasted about 40 s and the toast showed late. A rerun inside the same coalesced sync now reuses the token failure instead of waiting again. A rerun that unregisters needs no token and runs normally. - No server, wire, CLI or protocol changes. ## How you verified - New test `gives up on a push token request that never settles, once per coalesced sync, and runs the next sync`. Without the timeout it never finishes (vitest timeout at 15 s). Without the rerun change it fails (`expected { action: 'registered' } to deeply equal { action: 'failed', … }`). - `pnpm exec turbo run typecheck test lint lint:type-aware --continue --filter=@bb/mobile`: 8 tasks successful, 365 tests passed, 0 lint errors. - iOS 27.2 Simulator, local Debug dev-client build against a `start:worktree` server: - Before: with a temporary QA shim that holds every exp.host `fetch` open, the switch stayed on "Registering…" with a spinner for more than 3 minutes, and `pushSubscriptions.list` on the server was empty. - After: on a cold start and on toggling off and on, the spinner stopped at about 20 s with "Could not register: No push token from Apple or Expo after 20 s" and the toast "Could not turn on notifications". - Apple-side hang, without the shim: right after a simulator boot, the token request hung before any exp.host request. CFNetwork logged no exp.host connection, and apsd never logged the app. With the fix it failed after 20 s. Once apsd was up, toggling registered and the server listed the subscription ("On · registered with this server"). So the hang on vv2002's phone may be in Apple's device-token step rather than exp.host; the timeout covers both. Fixes get-bb#8392 > AGENT GENERATED 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
Each app resume runs two push syncs: the AppState
activehandler (it also reads the OS permission, #2881) and the effect that runs again when realtime reconnects (PushNotificationsHost.tsx). Each sync calledsyncPushRegistration, which callsgetExpoPushTokenAsyncbefore it checks the stored record. That call is always a POST tohttps://exp.host/--/api/v2/push/getExpoPushToken. The Expo push token changes only with the device token, and expo-notifications 58.1.8 already sends device token changes to Expo by itself (DevicePushTokenAutoRegistration.fx.ts, turned on by the firstgetExpoPushTokenAsync). So the phone made 2 exp.host round trips per resume to learn a token it already had. I confirmed the call pattern with the realcreatePushRegistrationControllerand counting fakes before changing anything (numbers below).What changed
apps/mobile/src/data/notifications/push-registration-controller.ts: the controller keeps the Expo push token for the app process and givessyncPushRegistrationthat value instead of a new fetch. It drops the token when the device token rolls (handleTokenRolled, after its existing duplicate check) and when Android releases the device token (releaseUnusedDeviceToken), so the next sync fetches again. Cold start still fetches once and compares the token with the stored record, as before. The 24 h re-registration still runs; it re-registers with the kept token.push-registration-controller.test.ts: a new test for the resume pattern and a token roll. Two tests had the old fetch counts. The permission-revoke test replacednotifications.getPermissionafter the controller was built; it now uses a permission variable insetup()and also asserts that the profile is unregistered.Deviation from the issue's idea (check the record age before the fetch, and set
registeredAt: 0on a token roll): that also gives 0 fetches per resume, but it stops the token check at cold start for up to 24 h, writes the persisted record on every roll, and does not cover the Android path where the controller releases the device token while other profiles keep a fresh record. The in-memory token has none of these gaps. If a token event arrives while a fetch is in flight, the in-flight result is kept; the Expo push token does not depend on the device token, so this can only keep the same value.How you verified
Before/after with the real controller and counting fakes (script kept out of the commit):
New test
fetches the Expo push token once per device token, not on every resume sync, run against the unfixed controller:With the fix: 9 passed. Each token reset is covered: without the reset in
releaseUnusedDeviceToken,waits for token deletion before registering a newly enabled serverfails; without the reset inhandleTokenRolled, the new test andignores repeated device token events and events caused by an active syncfail.pnpm exec turbo run typecheck test lint lint:type-aware --continue --filter=@bb/mobile: 8 tasks successful, 49 test files and 360 tests passed, lint 0 errors (6 existing warnings in other files), lint:type-aware 0 warnings.Fixes #8224