Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Expensify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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({
Expand All @@ -37,23 +62,9 @@ Onyx.connect({
},
});

function getLastUpdateIDAppliedToClient(): Promise<number> {
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<void> {
Log.info(`[PushNotification] Applying onyx data in the ${Visibility.isVisible() ? 'foreground' : 'background'}`, false, {reportID, reportActionID});

if (!ActiveClientManager.isClientTheLeader()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code only runs on native and this check is always true on native so this is actually dead code.

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});
Expand Down Expand Up @@ -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<number> {
return new Promise((resolve) => {
Onyx.connect({
key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT,
callback: (value) => resolve(value ?? CONST.DEFAULT_NUMBER_ID),
});
});
}
2 changes: 1 addition & 1 deletion tests/actions/SessionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading