Skip to content

Commit

Permalink
Avoid attempting to index into null NotificationRequest.trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
davidisaaclee committed Feb 6, 2025
1 parent 0cd60ae commit 7398b56
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 3 additions & 1 deletion apps/tlon-mobile/src/hooks/useNotificationListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function payloadFromNotification(
// `trigger`.
// Detect and use whatever payload is available.
const payload =
notification.request.trigger.type === 'push'
// `NotificationRequest.trigger` is marked as non-null in
// expo-notifications' types, but is null on Android - so we need the `?`
notification.request.trigger?.type === 'push'
? notification.request.trigger.payload
: notification.request.content.data;

Expand Down
16 changes: 11 additions & 5 deletions packages/app/lib/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ export const connectNotifications = async () => {
};

const channelIdFromNotification = (notif: Notifications.Notification) => {
if (notif.request.trigger.type !== 'push') {
return null;
let out: string | null = null;
if (
notif.request.trigger?.type === 'push' &&
typeof notif.request.trigger.payload?.channelId === 'string'
) {
out = notif.request.trigger.payload.channelId;
}
const out = notif.request.trigger.payload?.channelId;
if (typeof out !== 'string') {
return null;
if (
out == null &&
typeof notif.request.content.data?.channelId === 'string'
) {
out = notif.request.content.data.channelId;
}
return out;
};
Expand Down

0 comments on commit 7398b56

Please sign in to comment.