Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export default function Index() {
const response = await Notifications.getLastNotificationResponseAsync();
if (cancelled) return;
notificationRoute = consumeNotificationRoute(response);
Sentry.addBreadcrumb?.({
category: "notifications",
level: "info",
message: "Cold-start notification routing",
data: { hasResponse: response != null, route: notificationRoute },
});
} catch (error) {
Sentry.captureException(error);
}
Expand Down
27 changes: 22 additions & 5 deletions components/use-push-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const __resetPushNotificationsModuleStateForTests = () => {
indexInitialRoutingComplete = false;
};

const buildNotificationRoute = (data: Record<string, string>): string | null => {
const buildNotificationRoute = (data: Record<string, any>): string | null => {
if (!data.installment_id) return null;
const params = new URLSearchParams();
if (data.purchase_id) params.set("purchaseId", data.purchase_id);
Expand All @@ -89,11 +89,28 @@ export const consumeNotificationRoute = (
response: Notifications.NotificationResponse | null,
): string | null => {
if (!response) return null;
const identifier = response.notification.request.identifier;
const request = response.notification.request as {
identifier: string;
content?: { data?: Record<string, any> | null };
trigger?: { payload?: Record<string, any> | null } | null;
};
Comment on lines +92 to +96

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Overly-broad type cast makes content optional when the SDK guarantees it is always present

The cast widens request to a custom interface where content is optional (content?:). In the expo-notifications SDK, NotificationRequest.content is always defined — making it optional in the cast can mask TypeScript errors if future code accesses request.content without null-guarding. A safer approach is to cast only the trigger sub-field so the rest of the SDK-provided typing is preserved.

Suggested change
const request = response.notification.request as {
identifier: string;
content?: { data?: Record<string, any> | null };
trigger?: { payload?: Record<string, any> | null } | null;
};
const request = response.notification.request;
const trigger = request.trigger as { payload?: Record<string, any> | null } | null | undefined;

const identifier = request.identifier;
if (handledNotificationIdentifiers.has(identifier)) return null;
const data = response.notification.request.content.data as Record<string, string> | undefined;
const route = data ? buildNotificationRoute(data) : null;
if (!route) return null;
const payloads = [request.content?.data, request.trigger?.payload];
let route: string | null = null;
for (const payload of payloads) {
if (payload) route = buildNotificationRoute(payload);
if (route) break;
}
if (!route) {
Sentry.addBreadcrumb?.({
category: "notifications",
level: "warning",
message: "Notification tapped but no post route was built",
data: { contentData: request.content?.data ?? null, triggerPayload: request.trigger?.payload ?? null },
});
return null;
}
handledNotificationIdentifiers.add(identifier);
return route;
};
Expand Down
38 changes: 38 additions & 0 deletions tests/components/use-push-notifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,49 @@ describe("consumeNotificationRoute", () => {
).toBe("/post/post3?followerId=f3");
});

it("reads the payload from the iOS trigger when content.data is null", () => {
const response = {
notification: {
request: {
identifier: "ios-1",
content: { data: null },
trigger: { payload: { installment_id: "postIos", purchase_id: "pIos", aps: { alert: {} } } },
},
},
} as any;
expect(consumeNotificationRoute(response)).toBe("/post/postIos?purchaseId=pIos");
});

it("deduplicates by notification identifier", () => {
const response = makeResponse("dup-1", { installment_id: "post-dup" });
expect(consumeNotificationRoute(response)).toBe("/post/post-dup");
expect(consumeNotificationRoute(response)).toBeNull();
});

it("ignores the tag and message fields the Android FCM payload carries", () => {
expect(
consumeNotificationRoute(
makeResponse("a-1", {
installment_id: "postA",
purchase_id: "pA",
tag: "postA",
message: "New content added to product",
}),
),
).toBe("/post/postA?purchaseId=pA");
});

it("routes each of several distinct notifications (distinct tags are not deduped)", () => {
expect(consumeNotificationRoute(makeResponse("postA", { installment_id: "postA", tag: "postA" }))).toBe(
"/post/postA",
);
expect(consumeNotificationRoute(makeResponse("postB", { installment_id: "postB", tag: "postB" }))).toBe(
"/post/postB",
);
expect(consumeNotificationRoute(makeResponse("postC", { installment_id: "postC", tag: "postC" }))).toBe(
"/post/postC",
);
});
});

describe("usePushNotifications listener", () => {
Expand Down
Loading