From b6351e82b8ac175dd1c77dee7652f0e3a35b6828 Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Thu, 27 Mar 2025 16:58:36 -0400 Subject: [PATCH 1/6] remove unnecessary folder and rename lib --- src/Expensify.tsx | 2 +- .../index.ts => subscribeToPushNotifications.ts} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/libs/Notification/PushNotification/{subscribePushNotification/index.ts => subscribeToPushNotifications.ts} (99%) 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 99% rename from src/libs/Notification/PushNotification/subscribePushNotification/index.ts rename to src/libs/Notification/PushNotification/subscribeToPushNotifications.ts index f3984182d137b..0dbbb9e3ffe37 100644 --- a/src/libs/Notification/PushNotification/subscribePushNotification/index.ts +++ b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts @@ -13,7 +13,7 @@ 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 '.'; let lastVisitedPath: string | undefined; Onyx.connect({ From 67b3a820ef1277db28bfd9e6bfbc7f70898fc354 Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Thu, 27 Mar 2025 17:03:45 -0400 Subject: [PATCH 2/6] arrange functions high to low level --- .../subscribeToPushNotifications.ts | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts index 0dbbb9e3ffe37..c21462cea2c71 100644 --- a/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts +++ b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts @@ -15,6 +15,35 @@ import ROUTES from '@src/ROUTES'; import type {OnyxUpdatesFromServer} from '@src/types/onyx'; import PushNotification from '.'; +/** + * 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(); + } + }, +}); + let lastVisitedPath: string | undefined; Onyx.connect({ key: ONYXKEYS.LAST_VISITED_PATH, @@ -37,15 +66,6 @@ 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}); @@ -148,31 +168,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), + }); + }); +} From 430ddbf9abb20ae21cde1afb5dabe36a6a892dbc Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Thu, 27 Mar 2025 17:05:32 -0400 Subject: [PATCH 3/6] lint --- .../PushNotification/subscribeToPushNotifications.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts index c21462cea2c71..eabbd2fd22f17 100644 --- a/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts +++ b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts @@ -3,7 +3,6 @@ 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'; @@ -14,6 +13,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type {OnyxUpdatesFromServer} from '@src/types/onyx'; import PushNotification from '.'; +import type {ReportActionPushNotificationData} from './NotificationType'; /** * Manage push notification subscriptions on sign-in/sign-out. From 34efca168dab196c747ab7311276f872a0fe2d11 Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Thu, 27 Mar 2025 17:16:41 -0400 Subject: [PATCH 4/6] update import --- tests/actions/SessionTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'; From 6bb45b6cd6d7c9ac83a13f57693cc4248d960b1f Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Thu, 27 Mar 2025 17:29:59 -0400 Subject: [PATCH 5/6] remove unnecessary comment --- .../PushNotification/subscribeToPushNotifications.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts index eabbd2fd22f17..8f66bca4cefcd 100644 --- a/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts +++ b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts @@ -17,9 +17,6 @@ import type {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, From 2a628e5be5070ac67a304b85d4a255d0e31b9252 Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Fri, 28 Mar 2025 08:55:51 -0400 Subject: [PATCH 6/6] remove unnecessary active client check --- .../PushNotification/subscribeToPushNotifications.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts index 8f66bca4cefcd..f8f684302bd2d 100644 --- a/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts +++ b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts @@ -1,6 +1,5 @@ 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 {extractPolicyIDFromPath} from '@libs/PolicyUtils'; @@ -66,11 +65,6 @@ Onyx.connect({ 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});