Skip to content

Commit 9a995aa

Browse files
fix(mobile): a tokenless device stays registered for Live Activities
Report notification readiness separately from device registration, which Live Activities rely on and which do not need the device push token. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2799d13 commit 9a995aa

3 files changed

Lines changed: 20 additions & 13 deletions

File tree

‎apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type { Preferences } from "../../persistence/mobile-preferences";
3030
import { makeRelayDeviceRegistrationRequest } from "./registrationPayload";
3131
import {
3232
__resetAgentAwarenessRemoteRegistrationForTest,
33+
agentAwarenessNotificationsReady,
3334
getAgentAwarenessRegistrationStatus,
3435
refreshActiveLiveActivityRemoteRegistration,
3536
refreshAgentAwarenessRegistration,
@@ -736,7 +737,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
736737
return registrations;
737738
};
738739

739-
it.effect("does not report a device registered when its push token lookup fails", () => {
740+
it.effect("does not report notifications ready when its push token lookup fails", () => {
740741
const registrations = stubAndroidDeviceRelay();
741742
vi.mocked(Notifications.getDevicePushTokenAsync).mockRejectedValue(
742743
new Error("Default FirebaseApp is not initialized"),
@@ -746,7 +747,9 @@ describe("makeRelayDeviceRegistrationRequest", () => {
746747
return Effect.gen(function* () {
747748
yield* runBackgroundOperations();
748749

749-
expect(getAgentAwarenessRegistrationStatus()).toBe("failed");
750+
// Live Activities register with their own tokens, so the device stays registered.
751+
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
752+
expect(agentAwarenessNotificationsReady()).toBe(false);
750753
expect(registrations).toMatchObject([{ preferences: { notificationsEnabled: true } }]);
751754
expect(registrations[0]).not.toHaveProperty("pushToken");
752755
}).pipe(
@@ -774,7 +777,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
774777
yield* refreshAgentAwarenessRegistration();
775778

776779
expect(registrations).toMatchObject([{ preferences: { notificationsEnabled: true } }]);
777-
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
780+
expect(agentAwarenessNotificationsReady()).toBe(true);
778781

779782
// A lookup that started before a rotation must not replace the rotated token.
780783
let resolveStaleLookup: (token: { type: "android"; data: string }) => void = () => {};

‎apps/mobile/src/features/agent-awareness/remoteRegistration.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const registeredActivityPushTokens = new Map<string, number>();
9696
let widgetSnapshotReads = 0;
9797
let androidDeviceReplayedAt: number | null = null;
9898
let lastNativePushToken: string | null = null;
99+
let registeredWithPushToken = false;
99100
let nativePushTokenRotations = 0;
100101
let pushTokenSubscription: { remove: () => void } | null = null;
101102
let appStateSubscription: { remove: () => void } | null = null;
@@ -118,6 +119,12 @@ export function getAgentAwarenessRegistrationStatus(): AgentAwarenessRegistratio
118119
return registrationStatus;
119120
}
120121

122+
// Live Activities use their own push tokens, so device registration alone does not mean
123+
// the relay can deliver notifications.
124+
export function agentAwarenessNotificationsReady(): boolean {
125+
return registrationStatus === "registered" && registeredWithPushToken;
126+
}
127+
121128
export function subscribeAgentAwarenessRegistrationStatus(listener: () => void): () => void {
122129
registrationStatusListeners.add(listener);
123130
return () => {
@@ -368,13 +375,6 @@ function registrationSignature(body: RelayDeviceRegistrationRequest): string {
368375
].join("|");
369376
}
370377

371-
// Notifications are not set up until the relay holds a token for them.
372-
function acceptedRegistrationStatus(
373-
body: RelayDeviceRegistrationRequest,
374-
): AgentAwarenessRegistrationStatus {
375-
return body.preferences.notificationsEnabled && !body.pushToken ? "failed" : "registered";
376-
}
377-
378378
const registerDeviceWithRelay = Effect.fn("remoteRegistration.registerDeviceWithRelay")(function* (
379379
body: RelayDeviceRegistrationRequest,
380380
expectedGeneration: number,
@@ -423,7 +423,8 @@ const registerDeviceWithRelay = Effect.fn("remoteRegistration.registerDeviceWith
423423
persisted.signature === signature &&
424424
!needsAndroidReplay
425425
) {
426-
setRegistrationStatus(acceptedRegistrationStatus(payload));
426+
registeredWithPushToken = Boolean(payload.pushToken);
427+
setRegistrationStatus("registered");
427428
return;
428429
}
429430

@@ -439,7 +440,8 @@ const registerDeviceWithRelay = Effect.fn("remoteRegistration.registerDeviceWith
439440
return;
440441
}
441442
if (body.platform === "android") androidDeviceReplayedAt = yield* Clock.currentTimeMillis;
442-
setRegistrationStatus(acceptedRegistrationStatus(payload));
443+
registeredWithPushToken = Boolean(payload.pushToken);
444+
setRegistrationStatus("registered");
443445
yield* Effect.promise(() =>
444446
saveAgentAwarenessRegistrationRecord({
445447
identity,
@@ -873,6 +875,7 @@ export function __resetAgentAwarenessRemoteRegistrationForTest(): void {
873875
activeDeviceRegistration = null;
874876
pendingDeviceRegistration = null;
875877
registrationStatus = "unknown";
878+
registeredWithPushToken = false;
876879
androidDeviceReplayedAt = null;
877880
lastNativePushToken = null;
878881
nativePushTokenRotations = 0;

‎apps/mobile/src/features/settings/SettingsNotificationsRouteScreen.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
import { setLiveActivityUpdatesEnabled } from "../agent-awareness/liveActivityPreferences";
3535
import { requestAgentNotificationPermission } from "../agent-awareness/notificationPermissions";
3636
import {
37+
agentAwarenessNotificationsReady,
3738
getAgentAwarenessRegistrationStatus,
3839
refreshAgentAwarenessRegistration,
3940
subscribeAgentAwarenessRegistrationStatus,
@@ -192,7 +193,7 @@ function ConfiguredSettingsNotificationsRouteScreen() {
192193
}
193194
if (result.value.type === "granted") {
194195
setNotificationStatus("enabled");
195-
if (getAgentAwarenessRegistrationStatus() === "registered") {
196+
if (agentAwarenessNotificationsReady()) {
196197
Dialog.alert("Notifications enabled", "Agent notifications are enabled for this device.");
197198
} else {
198199
Dialog.alert(

0 commit comments

Comments
 (0)