diff --git a/src/libs/Notification/triggerNotifications.ts b/src/libs/Notification/triggerNotifications.ts new file mode 100644 index 000000000000..30b6f50637bf --- /dev/null +++ b/src/libs/Notification/triggerNotifications.ts @@ -0,0 +1,22 @@ +import type {OnyxCollection} from 'react-native-onyx'; +import {showReportActionNotification} from '@libs/actions/Report'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {ReportAction} from '@src/types/onyx'; +import type {OnyxServerUpdate} from '@src/types/onyx/OnyxUpdatesFromServer'; + +export default function triggerNotifications(onyxUpdates: OnyxServerUpdate[]): void { + for (const update of onyxUpdates) { + if (!update.shouldNotify && !update.shouldShowPushNotification) { + continue; + } + + const reportID = update.key.replace(ONYXKEYS.COLLECTION.REPORT_ACTIONS, ''); + const reportActions = Object.values((update.value as OnyxCollection) ?? {}); + + for (const action of reportActions) { + if (action) { + showReportActionNotification(reportID, action); + } + } + } +} diff --git a/src/libs/actions/OnyxUpdates.ts b/src/libs/actions/OnyxUpdates.ts index 053a0b739140..3feb758a8f07 100644 --- a/src/libs/actions/OnyxUpdates.ts +++ b/src/libs/actions/OnyxUpdates.ts @@ -1,8 +1,11 @@ +import {Platform} from 'react-native'; import type {OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import type {Merge} from 'type-fest'; import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; +import {isMobile} from '@libs/Browser'; import Log from '@libs/Log'; +import triggerNotifications from '@libs/Notification/triggerNotifications'; import Performance from '@libs/Performance'; import PusherUtils from '@libs/PusherUtils'; import CONST from '@src/CONST'; @@ -53,6 +56,10 @@ function applyHTTPSOnyxUpdates(request: Request, response: Response, lastUpdateI return onyxDataUpdatePromise .then(() => { + // Trigger notifications only on successful responses. + if (Platform.OS === 'web' && !isMobile() && response.jsonCode === 200 && response.onyxData?.length) { + triggerNotifications(response.onyxData); + } // Handle the request's success/failure data (client-side data) if (response.jsonCode === 200 && request.successData) { return updateHandler(request.successData); diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index e88ec32dc2ed..39e7487c72a2 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -34,6 +34,7 @@ import Log from '@libs/Log'; import Navigation from '@libs/Navigation/Navigation'; import {isOffline} from '@libs/Network/NetworkStore'; import * as SequentialQueue from '@libs/Network/SequentialQueue'; +import triggerNotifications from '@libs/Notification/triggerNotifications'; import * as NumberUtils from '@libs/NumberUtils'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import Pusher from '@libs/Pusher'; @@ -57,7 +58,6 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject'; import {reconnectApp} from './App'; import applyOnyxUpdatesReliably from './applyOnyxUpdatesReliably'; import {openOldDotLink} from './Link'; -import {showReportActionNotification} from './Report'; import {resendValidateCode as sessionResendValidateCode} from './Session'; import Timing from './Timing'; @@ -660,23 +660,6 @@ function isBlockedFromConcierge(blockedFromConciergeNVP: OnyxEntry) ?? {}); - - for (const action of reportActions) { - if (action) { - showReportActionNotification(reportID, action); - } - } - } -} - const isChannelMuted = (reportId: string) => new Promise((resolve) => { const connection = Onyx.connect({