Skip to content
TypeScript

fix(mobile): respect notification permission when tokens rotate (#10850)

Julius Marminge9d6c43f32ed2
diff --git a/apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts b/apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts
index 152948274ca..39bce6b4b90 100644
--- a/apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts
+++ b/apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts
@@ -924,4 +924,47 @@ describe("makeRelayDeviceRegistrationRequest", () => {
await new Promise((resolve) => setTimeout(resolve, 0));
expect(widgetMocks.start).toHaveBeenCalledTimes(1);
});
+ it.effect(
+ "does not enable notifications when a token rotates after permission is revoked",
+ () => {
+ const registrations: unknown[] = [];
+ vi.stubGlobal("fetch", async (input: RequestInfo | URL, init?: RequestInit) => {
+ const request = new Request(input, init);
+ if (request.url.endsWith("/v1/client/dpop-token")) {
+ return Response.json({
+ access_token: "dpop",
+ issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
+ token_type: "DPoP",
+ expires_in: 300,
+ scope: "mobile:registration",
+ });
+ }
+ registrations.push(await request.json());
+ return Response.json({ ok: true });
+ });
+ Constants.expoConfig!.extra = { relay: { url: "https://permission-relay.example.test" } };
+ setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk"), "user-a");
+ return Effect.gen(function* () {
+ yield* runBackgroundOperations();
+ expect(registrations.at(-1)).toMatchObject({ preferences: { notificationsEnabled: true } });
+ vi.mocked(Notifications.getPermissionsAsync).mockResolvedValueOnce({
+ granted: false,
+ } as Awaited<ReturnType<typeof Notifications.getPermissionsAsync>>);
+ const listener = vi.mocked(Notifications.addPushTokenListener).mock.calls.at(-1)![0];
+ listener({ type: "ios", data: "rotated" });
+ yield* runBackgroundOperations();
+ expect(registrations.at(-1)).toMatchObject({
+ preferences: { notificationsEnabled: false },
+ });
+ expect(registrations.at(-1)).not.toHaveProperty("pushToken");
+ }).pipe(
+ Effect.provideService(FetchHttpClient.Fetch, globalThis.fetch),
+ Effect.provide(
+ managedRelayClientLayer("https://permission-relay.example.test").pipe(
+ Layer.provide(Layer.mergeAll(FetchHttpClient.layer, cryptoLayer)),
+ ),
+ ),
+ );
+ },
+ );
});
diff --git a/apps/mobile/src/features/agent-awareness/remoteRegistration.ts b/apps/mobile/src/features/agent-awareness/remoteRegistration.ts
index 9ffd3178b31..14b1769c670 100644
--- a/apps/mobile/src/features/agent-awareness/remoteRegistration.ts
+++ b/apps/mobile/src/features/agent-awareness/remoteRegistration.ts
@@ -245,9 +245,6 @@ function nativePushTokenRegistration(observedPushToken?: string) {
if (!canRegisterRemoteLiveActivities() || !supportsAgentAwarenessPush()) {
return { notificationsEnabled: false, pushToken: null };
}
- if (observedPushToken) {
- return { notificationsEnabled: true, pushToken: observedPushToken };
- }
const permissions = yield* Effect.tryPromise({
try: () => Notifications.getPermissionsAsync(),
catch: (cause) =>
@@ -259,6 +256,9 @@ function nativePushTokenRegistration(observedPushToken?: string) {
if (!permissions.granted) {
return { notificationsEnabled: false, pushToken: null };
}
+ if (observedPushToken) {
+ return { notificationsEnabled: true, pushToken: observedPushToken };
+ }
const token = yield* Effect.tryPromise({
try: () => Notifications.getDevicePushTokenAsync(),
catch: (cause) =>