Skip to content

[No QA] Background refresh for Android #24128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
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';
Copy link
Contributor

Choose a reason for hiding this comment

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

This causes console error:

Screenshot 2023-08-13 at 10 26 16 PM

Copy link
Contributor

Choose a reason for hiding this comment

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

#24128 (comment) will be fixed in #24174

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