diff --git a/src/libs/Notification/PushNotification/subscribePushNotification/index.ts b/src/libs/Notification/PushNotification/subscribePushNotification/index.ts index b02ec2fd07e2..ec18f776d2d2 100644 --- a/src/libs/Notification/PushNotification/subscribePushNotification/index.ts +++ b/src/libs/Notification/PushNotification/subscribePushNotification/index.ts @@ -3,6 +3,7 @@ import applyOnyxUpdatesReliably from '@libs/actions/applyOnyxUpdatesReliably'; import * as ActiveClientManager from '@libs/ActiveClientManager'; import Log from '@libs/Log'; import Navigation from '@libs/Navigation/Navigation'; +import type {ReportActionPushNotificationData} from '@libs/Notification/PushNotification/NotificationType'; import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils'; import {extractPolicyIDFromPath} from '@libs/PolicyUtils'; import {doesReportBelongToWorkspace, getReport} from '@libs/ReportUtils'; @@ -14,33 +15,6 @@ import ROUTES from '@src/ROUTES'; import type {OnyxUpdatesFromServer} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import PushNotification from '..'; -import {ReportActionPushNotificationData} from '../NotificationType'; - -/** - * Manage push notification subscriptions on sign-in/sign-out. - * - * On Android, AuthScreens unmounts when the app is closed with the back button so we manage the - * push subscription when the session changes here. - */ -Onyx.connect({ - key: ONYXKEYS.NVP_PRIVATE_PUSH_NOTIFICATION_ID, - callback: (notificationID) => { - if (notificationID) { - PushNotification.register(notificationID); - PushNotification.init(); - - // Subscribe handlers for different push notification types - PushNotification.onReceived(PushNotification.TYPE.REPORT_COMMENT, applyOnyxData); - PushNotification.onSelected(PushNotification.TYPE.REPORT_COMMENT, navigateToReport); - - PushNotification.onReceived(PushNotification.TYPE.MONEY_REQUEST, applyOnyxData); - PushNotification.onSelected(PushNotification.TYPE.MONEY_REQUEST, navigateToReport); - } else { - PushNotification.deregister(); - PushNotification.clearNotifications(); - } - }, -}); let lastVisitedPath: string | undefined; Onyx.connect({ @@ -53,6 +27,15 @@ Onyx.connect({ }, }); +function getLastUpdateIDAppliedToClient(): Promise { + return new Promise((resolve) => { + Onyx.connect({ + key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, + callback: (value) => resolve(value ?? 0), + }); + }); +} + function applyOnyxData({reportID, reportActionID, onyxData, lastUpdateID, previousUpdateID}: ReportActionPushNotificationData): Promise { Log.info(`[PushNotification] Applying onyx data in the ${Visibility.isVisible() ? 'foreground' : 'background'}`, false, {reportID, reportActionID}); @@ -125,11 +108,28 @@ function navigateToReport({reportID, reportActionID}: ReportActionPushNotificati return Promise.resolve(); } -function getLastUpdateIDAppliedToClient(): Promise { - return new Promise((resolve) => { - Onyx.connect({ - key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, - callback: (value) => resolve(value ?? 0), - }); - }); -} +/** + * Manage push notification subscriptions on sign-in/sign-out. + * + * On Android, AuthScreens unmounts when the app is closed with the back button so we manage the + * push subscription when the session changes here. + */ +Onyx.connect({ + key: ONYXKEYS.NVP_PRIVATE_PUSH_NOTIFICATION_ID, + callback: (notificationID) => { + if (notificationID) { + PushNotification.register(notificationID); + PushNotification.init(); + + // Subscribe handlers for different push notification types + PushNotification.onReceived(PushNotification.TYPE.REPORT_COMMENT, applyOnyxData); + PushNotification.onSelected(PushNotification.TYPE.REPORT_COMMENT, navigateToReport); + + PushNotification.onReceived(PushNotification.TYPE.MONEY_REQUEST, applyOnyxData); + PushNotification.onSelected(PushNotification.TYPE.MONEY_REQUEST, navigateToReport); + } else { + PushNotification.deregister(); + PushNotification.clearNotifications(); + } + }, +});