Skip to content
Merged
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
26 changes: 20 additions & 6 deletions hooks/usePushNotifications.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { useEffect } from 'react';
import { Platform } from 'react-native';
import * as Notifications from 'expo-notifications';
import { useRouter } from 'expo-router';
import { Href, useRouter } from 'expo-router';
import messaging from '@react-native-firebase/messaging';

import { path } from '@/constants/path';
import { registerPushToken } from '@/lib/api';
import { registerForPushNotificationsAsync } from '@/lib/registerForPushNotifications';
import { useUserStore } from '@/store/useUserStore';

/**
* Map a push notification's `type` (set by the backend) to an in-app route.
* Card payment notifications open the card screen; anything else goes home.
*/
function getNotificationRoute(type?: string): Href {
switch (type) {
case 'card-transaction':
return path.CARD;
default:
return path.HOME;
}
}

/**
* Manages push notification lifecycle: token refresh and notification tap handling.
* Must be mounted inside the root layout so listeners are active for the entire session.
Expand Down Expand Up @@ -39,12 +52,13 @@ export function usePushNotifications() {
}
});

// Handle notification taps (user taps a notification from the system tray)
// Handle notification taps (user taps a notification from the system tray).
// Deep-link based on the `type` the backend set in the notification data;
// fall back to home for anything unrecognised.
const notificationResponseSubscription = Notifications.addNotificationResponseReceivedListener(
_response => {
// Future: read _response.notification.request.content.data.route for deep linking
// e.g., router.replace(_response.notification.request.content.data.route as any);
router.replace(path.HOME);
response => {
const data = response.notification.request.content.data as { type?: string } | undefined;
router.replace(getNotificationRoute(data?.type));
},
);

Expand Down
Loading