-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Only runs when the user is logged in and has passed the pin screen. - Improves notification handling to support passkey browser.
- Loading branch information
Showing
11 changed files
with
147 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import messaging, { | ||
FirebaseMessagingTypes, | ||
} from '@react-native-firebase/messaging' | ||
import { useCallback, useEffect, useRef, useState } from 'react' | ||
import { useURL } from 'expo-linking' | ||
import { useMarkUserNotificationAsReadMutation } from '../graphql/types/schema' | ||
|
||
import { navigateToUniversalLink } from '../lib/deep-linking' | ||
import { useBrowser } from '../lib/use-browser' | ||
import { useAuthStore } from '../stores/auth-store' | ||
|
||
// Expo-style notification hook wrapping firebase. | ||
function useLastNotificationResponse() { | ||
const [lastNotificationResponse, setLastNotificationResponse] = | ||
useState<FirebaseMessagingTypes.RemoteMessage | null>(null) | ||
|
||
useEffect(() => { | ||
messaging() | ||
.getInitialNotification() | ||
.then((remoteMessage) => { | ||
if (remoteMessage) { | ||
setLastNotificationResponse(remoteMessage) | ||
} | ||
}) | ||
|
||
// Return the unsubscribe function as a useEffect destructor. | ||
return messaging().onNotificationOpenedApp((remoteMessage) => { | ||
setLastNotificationResponse(remoteMessage) | ||
}) | ||
}, []) | ||
|
||
return lastNotificationResponse | ||
} | ||
|
||
export function useDeepLinkHandling() { | ||
const url = useURL() | ||
const notification = useLastNotificationResponse() | ||
const [markUserNotificationAsRead] = useMarkUserNotificationAsReadMutation() | ||
const lockScreenActivatedAt = useAuthStore( | ||
({ lockScreenActivatedAt }) => lockScreenActivatedAt, | ||
) | ||
|
||
const lastUrl = useRef<string | null>(null) | ||
const { openBrowser } = useBrowser() | ||
|
||
const handleUrl = useCallback( | ||
(url?: string | null) => { | ||
if (!url || lastUrl.current === url || lockScreenActivatedAt) { | ||
return false | ||
} | ||
lastUrl.current = url | ||
|
||
navigateToUniversalLink({ link: url, openBrowser }) | ||
return true | ||
}, | ||
[openBrowser, lastUrl, lockScreenActivatedAt], | ||
) | ||
|
||
useEffect(() => { | ||
handleUrl(url) | ||
}, [url, handleUrl]) | ||
|
||
useEffect(() => { | ||
const url = notification?.data?.clickActionUrl | ||
const wasHandled = handleUrl(url) | ||
if (wasHandled && notification?.data?.notificationId) { | ||
// Mark notification as read and seen | ||
void markUserNotificationAsRead({ | ||
variables: { id: Number(notification.data.notificationId) }, | ||
}) | ||
} | ||
}, [notification, handleUrl, markUserNotificationAsRead]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.