From b38d73b38c22adb6210c074f058c8825d037731a Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Thu, 3 Aug 2023 12:57:03 -0400 Subject: [PATCH 1/4] add background refresh for android --- .../backgroundRefresh/index.android.js | 15 +++++++++++++++ .../PushNotification/backgroundRefresh/index.js | 7 +++++++ .../subscribeToReportCommentPushNotifications.js | 2 ++ 3 files changed, 24 insertions(+) create mode 100644 src/libs/Notification/PushNotification/backgroundRefresh/index.android.js create mode 100644 src/libs/Notification/PushNotification/backgroundRefresh/index.js 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..cf482e72126a --- /dev/null +++ b/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js @@ -0,0 +1,15 @@ +import * as App from '../../../actions/App'; +import Visibility from '../../../Visibility'; +/** + * 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; + } + + App.reconnectApp(); +} 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 From a73c5356a915f1b5b10e0d14fb74d10b5cc3f654 Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Fri, 4 Aug 2023 13:46:04 -0400 Subject: [PATCH 2/4] use new lastUpdateID to do a quick reconnect --- .../backgroundRefresh/index.android.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js b/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js index cf482e72126a..ac9822dd04dd 100644 --- a/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js +++ b/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js @@ -1,5 +1,20 @@ +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. * @@ -11,5 +26,5 @@ export default function backgroundRefresh() { return; } - App.reconnectApp(); + getLastOnyxUpdateID().then((lastUpdateID) => App.reconnectApp(lastUpdateID)); } From 7028add241c2ac3743c0293e5d8f37dbcb7ee96d Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Fri, 4 Aug 2023 17:18:45 -0400 Subject: [PATCH 3/4] call confirmReadyToOpenApp to allow reconnectApp to run --- .../PushNotification/backgroundRefresh/index.android.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js b/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js index ac9822dd04dd..7bef99ad74a7 100644 --- a/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js +++ b/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js @@ -26,5 +26,11 @@ export default function backgroundRefresh() { return; } - getLastOnyxUpdateID().then((lastUpdateID) => App.reconnectApp(lastUpdateID)); + getLastOnyxUpdateID().then((lastUpdateID) => { + // ReconnectApp waits on our isReadyToOpenApp promise to resolve and this normally only resolves when the LHN is rendered. + // However, on Android this is run in the background using a HeadlessJS task (https://reactnative.dev/docs/headless-js-android) + // which does not render the React UI. So we must manually run confirmReadyToOpenApp here instead + App.confirmReadyToOpenApp(); + App.reconnectApp(lastUpdateID); + }); } From 02c6981437037686f4478d969ab8ec5405a0f871 Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Fri, 4 Aug 2023 17:32:25 -0400 Subject: [PATCH 4/4] clearer comment --- .../backgroundRefresh/index.android.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js b/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js index 7bef99ad74a7..d86589e2f207 100644 --- a/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js +++ b/src/libs/Notification/PushNotification/backgroundRefresh/index.android.js @@ -27,9 +27,13 @@ export default function backgroundRefresh() { } getLastOnyxUpdateID().then((lastUpdateID) => { - // ReconnectApp waits on our isReadyToOpenApp promise to resolve and this normally only resolves when the LHN is rendered. - // However, on Android this is run in the background using a HeadlessJS task (https://reactnative.dev/docs/headless-js-android) - // which does not render the React UI. So we must manually run confirmReadyToOpenApp here instead + /** + * 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); });