Skip to content

Commit

Permalink
Merge pull request #24128 from Expensify/arosiclair-android-backgroun…
Browse files Browse the repository at this point in the history
…d-refresh

[No QA] Background refresh for Android
  • Loading branch information
arosiclair authored Aug 10, 2023
2 parents 7bb5239 + e5fab7d commit b8119e9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
});
}
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit b8119e9

Please sign in to comment.