Restructure notification/linking handling to use synchronous APIs (#9497)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Eric Bailey <git@esb.lol>
This commit is contained in:
+21
-25
@@ -1,5 +1,5 @@
|
|||||||
import {type JSX, useCallback, useRef} from 'react'
|
import {type JSX, useCallback, useRef} from 'react'
|
||||||
import {Linking} from 'react-native'
|
import * as Linking from 'expo-linking'
|
||||||
import * as Notifications from 'expo-notifications'
|
import * as Notifications from 'expo-notifications'
|
||||||
import {i18n, type MessageDescriptor} from '@lingui/core'
|
import {i18n, type MessageDescriptor} from '@lingui/core'
|
||||||
import {msg} from '@lingui/macro'
|
import {msg} from '@lingui/macro'
|
||||||
@@ -874,11 +874,6 @@ const LINKING = {
|
|||||||
},
|
},
|
||||||
} satisfies LinkingOptions<AllNavigatorParams>
|
} satisfies LinkingOptions<AllNavigatorParams>
|
||||||
|
|
||||||
/**
|
|
||||||
* Used to ensure we don't handle the same notification twice
|
|
||||||
*/
|
|
||||||
let lastHandledNotificationDateDedupe: number | undefined
|
|
||||||
|
|
||||||
function RoutesContainer({children}: React.PropsWithChildren<{}>) {
|
function RoutesContainer({children}: React.PropsWithChildren<{}>) {
|
||||||
const ax = useAnalytics()
|
const ax = useAnalytics()
|
||||||
const notyLogger = ax.logger.useChild(ax.logger.Context.Notifications)
|
const notyLogger = ax.logger.useChild(ax.logger.Context.Notifications)
|
||||||
@@ -889,6 +884,8 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) {
|
|||||||
const previousScreen = useRef<string | undefined>(undefined)
|
const previousScreen = useRef<string | undefined>(undefined)
|
||||||
const emailDialogControl = useEmailDialogControl()
|
const emailDialogControl = useEmailDialogControl()
|
||||||
const closeAllActiveElements = useCloseAllActiveElements()
|
const closeAllActiveElements = useCloseAllActiveElements()
|
||||||
|
const linkingUrl = Linking.useLinkingURL()
|
||||||
|
const notificationResponse = Notifications.useLastNotificationResponse()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle navigation to a conversation, or prepares for account switch.
|
* Handle navigation to a conversation, or prepares for account switch.
|
||||||
@@ -923,29 +920,28 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
async function handlePushNotificationEntry() {
|
function handlePushNotificationEntry() {
|
||||||
if (!IS_NATIVE) return
|
if (!IS_NATIVE) return
|
||||||
|
|
||||||
// deep links take precedence - on android,
|
// intent urls are handled by `useIntentHandler`
|
||||||
// getLastNotificationResponseAsync returns a "notification"
|
if (linkingUrl) return
|
||||||
// that is actually a deep link. avoid handling it twice -sfn
|
|
||||||
if (await Linking.getInitialURL()) {
|
if (notificationResponse) {
|
||||||
return
|
notyLogger.debug(`handlePushNotificationEntry: response`, {
|
||||||
|
response: notificationResponse,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Clear the last notification response to ensure it's not used again
|
||||||
|
try {
|
||||||
|
Notifications.clearLastNotificationResponse()
|
||||||
|
} catch (error) {
|
||||||
|
notyLogger.error(
|
||||||
|
`handlePushNotificationEntry: error clearing notification response`,
|
||||||
|
{error},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
const payload = getNotificationPayload(notificationResponse.notification)
|
||||||
* The notification that caused the app to open, if applicable
|
|
||||||
*/
|
|
||||||
const response = await Notifications.getLastNotificationResponseAsync()
|
|
||||||
|
|
||||||
if (response) {
|
|
||||||
notyLogger.debug(`handlePushNotificationEntry: response`, {response})
|
|
||||||
|
|
||||||
if (response.notification.date === lastHandledNotificationDateDedupe)
|
|
||||||
return
|
|
||||||
lastHandledNotificationDateDedupe = response.notification.date
|
|
||||||
|
|
||||||
const payload = getNotificationPayload(response.notification)
|
|
||||||
|
|
||||||
if (payload) {
|
if (payload) {
|
||||||
ax.metric('notifications:openApp', {
|
ax.metric('notifications:openApp', {
|
||||||
|
|||||||
Reference in New Issue
Block a user