From 6c728f79dea65d9deeb57a430e6c9fad39940316 Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 3 Apr 2024 23:42:28 +0100 Subject: [PATCH] Make notifications init reactive to queryClient (#3329) --- src/App.native.tsx | 4 +- src/lib/notifications/notifications.ts | 104 ++++++++++++++----------- 2 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/App.native.tsx b/src/App.native.tsx index 2c880f2178..36944aa911 100644 --- a/src/App.native.tsx +++ b/src/App.native.tsx @@ -19,8 +19,8 @@ import {init as initPersistedState} from '#/state/persisted' import * as persisted from '#/state/persisted' import {Provider as LabelDefsProvider} from '#/state/preferences/label-defs' import {useIntentHandler} from 'lib/hooks/useIntentHandler' +import {useNotificationsListener} from 'lib/notifications/notifications' import {useOTAUpdates} from 'lib/hooks/useOTAUpdates' -import * as notifications from 'lib/notifications/notifications' import { asyncStoragePersister, dehydrateOptions, @@ -61,11 +61,11 @@ function InnerApp() { const theme = useColorModeTheme() const {_} = useLingui() useIntentHandler() + useNotificationsListener(queryClient) useOTAUpdates() // init useEffect(() => { - notifications.init(queryClient) listenSessionDropped(() => { Toast.show(_(msg`Sorry! Your session expired. Please log in again.`)) }) diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index e811f690ed..0f628f4288 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -1,12 +1,14 @@ +import {useEffect} from 'react' import * as Notifications from 'expo-notifications' import {QueryClient} from '@tanstack/react-query' -import {resetToTab} from '../../Navigation' -import {devicePlatform, isIOS} from 'platform/detection' -import {track} from 'lib/analytics/analytics' + import {logger} from '#/logger' import {RQKEY as RQKEY_NOTIFS} from '#/state/queries/notifications/feed' import {truncateAndInvalidate} from '#/state/queries/util' -import {SessionAccount, getAgent} from '#/state/session' +import {getAgent, SessionAccount} from '#/state/session' +import {track} from 'lib/analytics/analytics' +import {devicePlatform, isIOS} from 'platform/detection' +import {resetToTab} from '../../Navigation' import {logEvent} from '../statsig/statsig' const SERVICE_DID = (serviceUrl?: string) => @@ -80,53 +82,63 @@ export function registerTokenChangeHandler( } } -export function init(queryClient: QueryClient) { - // handle notifications that are received, both in the foreground or background - // NOTE: currently just here for debug logging - Notifications.addNotificationReceivedListener(event => { - logger.debug( - 'Notifications: received', - {event}, - logger.DebugContext.notifications, - ) - if (event.request.trigger.type === 'push') { - // handle payload-based deeplinks - let payload - if (isIOS) { - payload = event.request.trigger.payload - } else { - // TODO: handle android payload deeplink +export function useNotificationsListener(queryClient: QueryClient) { + useEffect(() => { + // handle notifications that are received, both in the foreground or background + // NOTE: currently just here for debug logging + const sub1 = Notifications.addNotificationReceivedListener(event => { + logger.debug( + 'Notifications: received', + {event}, + logger.DebugContext.notifications, + ) + if (event.request.trigger.type === 'push') { + // handle payload-based deeplinks + let payload + if (isIOS) { + payload = event.request.trigger.payload + } else { + // TODO: handle android payload deeplink + } + if (payload) { + logger.debug( + 'Notifications: received payload', + payload, + logger.DebugContext.notifications, + ) + // TODO: deeplink notif here + } } - if (payload) { + }) + + // handle notifications that are tapped on + const sub2 = Notifications.addNotificationResponseReceivedListener( + response => { logger.debug( - 'Notifications: received payload', - payload, + 'Notifications: response received', + { + actionIdentifier: response.actionIdentifier, + }, logger.DebugContext.notifications, ) - // TODO: deeplink notif here - } - } - }) - - // handle notifications that are tapped on - Notifications.addNotificationResponseReceivedListener(response => { - logger.debug( - 'Notifications: response received', - { - actionIdentifier: response.actionIdentifier, + if ( + response.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER + ) { + logger.debug( + 'User pressed a notification, opening notifications tab', + {}, + logger.DebugContext.notifications, + ) + track('Notificatons:OpenApp') + logEvent('notifications:openApp', {}) + truncateAndInvalidate(queryClient, RQKEY_NOTIFS()) + resetToTab('NotificationsTab') // open notifications tab + } }, - logger.DebugContext.notifications, ) - if (response.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER) { - logger.debug( - 'User pressed a notification, opening notifications tab', - {}, - logger.DebugContext.notifications, - ) - track('Notificatons:OpenApp') - logEvent('notifications:openApp', {}) - truncateAndInvalidate(queryClient, RQKEY_NOTIFS()) - resetToTab('NotificationsTab') // open notifications tab + return () => { + sub1.remove() + sub2.remove() } - }) + }, [queryClient]) }