-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24128 from Expensify/arosiclair-android-backgroun…
…d-refresh [No QA] Background refresh for Android
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/libs/Notification/PushNotification/backgroundRefresh/index.android.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/libs/Notification/PushNotification/backgroundRefresh/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters