diff --git a/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js b/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js new file mode 100644 index 000000000000..d86589e2f207 --- /dev/null +++ b/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js @@ -0,0 +1,40 @@ +import Onyx from 'react-native-onyx'; +import * as App from '../../../actions/App'; +import Visibility from '../../../Visibility'; +import ONYXKEYS from '../../../../ONYXKEYS'; + +function getLastOnyxUpdateID() { + return new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.ONYX_UPDATES.LAST_UPDATE_ID, + callback: (lastUpdateID) => { + Onyx.disconnect(connectionID); + resolve(lastUpdateID); + }, + }); + }); +} + +/** + * Runs our reconnectApp action if the app is in the background. + * + * We use this to refresh the app in the background after receiving a push notification (native only). Since the full app + * wakes on iOS and by extension runs reconnectApp already, this is a no-op on everything but Android. + */ +export default function backgroundRefresh() { + if (Visibility.isVisible()) { + return; + } + + getLastOnyxUpdateID().then((lastUpdateID) => { + /** + * ReconnectApp waits on the isReadyToOpenApp promise to resolve and this normally only resolves when the LHN is rendered. + * However on Android, this callback is run in the background using a Headless JS task which does not render the React UI, + * so we must manually run confirmReadyToOpenApp here instead. + * + * See more here: https://reactnative.dev/docs/headless-js-android + */ + App.confirmReadyToOpenApp(); + App.reconnectApp(lastUpdateID); + }); +} diff --git a/src/libs/Notification/PushNotification/backgroundRefresh/index.js b/src/libs/Notification/PushNotification/backgroundRefresh/index.js new file mode 100644 index 000000000000..657fb15ee429 --- /dev/null +++ b/src/libs/Notification/PushNotification/backgroundRefresh/index.js @@ -0,0 +1,7 @@ +/** + * Runs our reconnectApp action if the app is in the background. + * + * We use this to refresh the app in the background after receiving a push notification (native only). Since the full app + * wakes on iOS and by extension runs reconnectApp already, this is a no-op on everything but Android. + */ +export default function backgroundRefresh() {} diff --git a/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.js b/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.js index 4effe9a8894d..a36fef610a39 100644 --- a/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.js +++ b/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.js @@ -4,6 +4,7 @@ import ROUTES from '../../../ROUTES'; import Log from '../../Log'; import Navigation from '../../Navigation/Navigation'; import Visibility from '../../Visibility'; +import backgroundRefresh from './backgroundRefresh'; /** * Setup reportComment push notification callbacks. @@ -12,6 +13,7 @@ export default function subscribeToReportCommentPushNotifications() { PushNotification.onReceived(PushNotification.TYPE.REPORT_COMMENT, ({reportID, reportActionID, onyxData}) => { Log.info(`[PushNotification] received report comment notification in the ${Visibility.isVisible() ? 'foreground' : 'background'}`, false, {reportID, reportActionID}); Onyx.update(onyxData); + backgroundRefresh(); }); // Open correct report when push notification is clicked