diff --git a/src/Expensify.tsx b/src/Expensify.tsx index 135423808885d..fc1b604422a32 100644 --- a/src/Expensify.tsx +++ b/src/Expensify.tsx @@ -32,7 +32,7 @@ import Navigation from './libs/Navigation/Navigation'; import NavigationRoot from './libs/Navigation/NavigationRoot'; import NetworkConnection from './libs/NetworkConnection'; import PushNotification from './libs/Notification/PushNotification'; -import './libs/Notification/PushNotification/subscribePushNotification'; +import './libs/Notification/PushNotification/subscribeToPushNotifications'; import setCrashlyticsUserId from './libs/setCrashlyticsUserId'; import StartupTimer from './libs/StartupTimer'; // This lib needs to be imported, but it has nothing to export since all it contains is an Onyx connection diff --git a/src/libs/Notification/PushNotification/subscribePushNotification/index.ts b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts similarity index 92% rename from src/libs/Notification/PushNotification/subscribePushNotification/index.ts rename to src/libs/Notification/PushNotification/subscribeToPushNotifications.ts index f3984182d137b..f8f684302bd2d 100644 --- a/src/libs/Notification/PushNotification/subscribePushNotification/index.ts +++ b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts @@ -1,9 +1,7 @@ import Onyx from 'react-native-onyx'; 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 {extractPolicyIDFromPath} from '@libs/PolicyUtils'; import Visibility from '@libs/Visibility'; import {updateLastVisitedPath} from '@userActions/App'; @@ -13,7 +11,34 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type {OnyxUpdatesFromServer} from '@src/types/onyx'; -import PushNotification from '..'; +import PushNotification from '.'; +import type {ReportActionPushNotificationData} from './NotificationType'; + +/** + * Manage push notification subscriptions on sign-in/sign-out. + */ +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); + + PushNotification.onReceived(PushNotification.TYPE.REPORT_ACTION, applyOnyxData); + PushNotification.onSelected(PushNotification.TYPE.REPORT_ACTION, navigateToReport); + } else { + PushNotification.deregister(); + PushNotification.clearNotifications(); + } + }, +}); let lastVisitedPath: string | undefined; Onyx.connect({ @@ -37,23 +62,9 @@ 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 ?? CONST.DEFAULT_NUMBER_ID), - }); - }); -} - function applyOnyxData({reportID, reportActionID, onyxData, lastUpdateID, previousUpdateID, hasPendingOnyxUpdates = false}: ReportActionPushNotificationData): Promise { Log.info(`[PushNotification] Applying onyx data in the ${Visibility.isVisible() ? 'foreground' : 'background'}`, false, {reportID, reportActionID}); - if (!ActiveClientManager.isClientTheLeader()) { - Log.info('[PushNotification] received report comment notification, but ignoring it since this is not the active client'); - return Promise.resolve(); - } - const logMissingOnyxDataInfo = (isDataMissing: boolean): boolean => { if (isDataMissing) { Log.hmmm("[PushNotification] didn't apply onyx updates because some data is missing", {lastUpdateID, previousUpdateID, onyxDataCount: onyxData?.length ?? 0}); @@ -148,31 +159,11 @@ function navigateToReport({reportID, reportActionID}: ReportActionPushNotificati return Promise.resolve(); } -/** - * 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); - - PushNotification.onReceived(PushNotification.TYPE.REPORT_ACTION, applyOnyxData); - PushNotification.onSelected(PushNotification.TYPE.REPORT_ACTION, navigateToReport); - } else { - PushNotification.deregister(); - PushNotification.clearNotifications(); - } - }, -}); +function getLastUpdateIDAppliedToClient(): Promise { + return new Promise((resolve) => { + Onyx.connect({ + key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, + callback: (value) => resolve(value ?? CONST.DEFAULT_NUMBER_ID), + }); + }); +} diff --git a/tests/actions/SessionTest.ts b/tests/actions/SessionTest.ts index 24f1945456b86..fc86a42d0b11e 100644 --- a/tests/actions/SessionTest.ts +++ b/tests/actions/SessionTest.ts @@ -11,7 +11,7 @@ import asyncOpenURL from '@libs/asyncOpenURL'; import HttpUtils from '@libs/HttpUtils'; import PushNotification from '@libs/Notification/PushNotification'; // This lib needs to be imported, but it has nothing to export since all it contains is an Onyx connection -import '@libs/Notification/PushNotification/subscribePushNotification'; +import '@libs/Notification/PushNotification/subscribeToPushNotifications'; import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; import * as SessionUtil from '@src/libs/actions/Session';