Project Setup
Add @codeonair/code-push-react-native to a React Native 0.70+ app on Android and iOS: keys, bundle loading, CodePush.sync() and install modes.
Four changes: install the package, add your keys, load the update's bundle in native code, and call CodePush.sync() when the app starts.
Install#
The SDK is on npm as @codeonair/code-push-react-native (currently 1.1.0).
npm install @codeonair/code-push-react-native cd ios && pod install
Android#
1. Keys
<resources> <string name="app_name">MyApp</string> <string moduleConfig="true" name="CodePushDeploymentKey">cpd_…</string> <string moduleConfig="true" name="CodePushServerUrl">https://app.codeonair.com</string> </resources>
2. Load the update's bundle (React Native 0.77+)
import com.codeonair.codepush.CodeOnAirCodePush
override val reactHost: ReactHost by lazy {
getDefaultReactHost(
context = applicationContext,
packageList = PackageList(this).packages,
jsBundleFilePath = CodeOnAirCodePush.getJSBundleFile(applicationContext),
)
}React Native 0.70–0.76
These apps have a DefaultReactNativeHost in MainApplication: override getJSBundleFile() instead. It covers the Old and the New Architecture.
import com.codeonair.codepush.CodeOnAirCodePush
override val reactNativeHost: ReactNativeHost =
object : DefaultReactNativeHost(this) {
// …getPackages(), getJSMainModuleName() as before…
override fun getJSBundleFile(): String? = CodeOnAirCodePush.getJSBundleFile(applicationContext)
}iOS#
1. Keys
<key>CodePushDeploymentKey</key> <string>cpd_…</string> <key>CodePushServerURL</key> <string>https://app.codeonair.com</string>
2. Load the update's bundle (React Native 0.77+)
import CodeOnAirCodePush
override func bundleURL() -> URL? {
#if DEBUG
RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
#else
CodeOnAirCodePush.bundleURL()
#endif
}React Native 0.70–0.76
These apps have an Objective-C AppDelegate.mm: return the CodePush bundle from bundleURL. It covers the Old and the New Architecture.
#import <CodeOnAirCodePush/CodeOnAirCodePush.h>
- (NSURL *)bundleURL
{
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
return [CodeOnAirCodePush bundleURL];
#endif
}Sync on launch#
Call CodePush.sync() once when the app starts — in your root component. It first confirms the running update (so it isn't rolled back), then checks for a new one. Call it on every launch, not from a screen that might not open.
import CodePush from "@codeonair/code-push-react-native";
function App() {
useEffect(() => {
// Every launch: confirms the running update, then checks for a new one.
CodePush.sync(
{
installMode: CodePush.InstallMode.ON_NEXT_RESTART, // optional updates: on the next app start
mandatoryInstallMode: CodePush.InstallMode.IMMEDIATE, // Mandatory builds: right away
},
(status) => {
if (status === CodePush.SyncStatus.UPDATE_INSTALLED) console.log("Update installed.");
},
);
}, []);
// …
}
export default App;Choose when updates run#
| Install mode | When the update runs | Use it for |
|---|---|---|
ON_NEXT_RESTART | The next time the app is started. | Most releases — users never see a reload. |
IMMEDIATE | Right away: the app restarts as soon as it's downloaded. | Urgent fixes — usually through mandatory releases. |
ON_NEXT_RESUME | When the app next comes back from the background. | Apps that stay open for days. |
ON_NEXT_SUSPEND | While the app is in the background — the user comes back to the new version already running. | Mandatory releases without a visible reload. |
installMode applies to optional releases and mandatoryInstallMode to releases marked Mandatory (default: immediate). Either can be any of the four modes. Add updateDialog: true to ask the user before installing.
Mandatory updates without a visible reload
The defaults haven't changed — this keeps working exactly as before, and a mandatory update restarts the app as soon as it's downloaded:
CodePush.sync({
installMode: CodePush.InstallMode.ON_NEXT_RESTART,
mandatoryInstallMode: CodePush.InstallMode.IMMEDIATE,
});To keep the user from ever seeing that reload, install mandatory updates with ON_NEXT_SUSPEND: the update installs right away, and the app restarts onto it the next time it's in the background. Add restoreScreen so the user lands on the screen they left:
import codePush from "@codeonair/code-push-react-native";
import { NavigationContainer } from "@react-navigation/native";
function App() {
return (
<NavigationContainer
initialState={codePush.restoredScreenState} // the screen from before a CodePush restart, else undefined
onStateChange={codePush.trackScreenState}>
{/* … your navigators … */}
</NavigationContainer>
);
}
export default codePush({
installMode: codePush.InstallMode.ON_NEXT_RESTART,
mandatoryInstallMode: codePush.InstallMode.ON_NEXT_SUSPEND,
minimumBackgroundDuration: 10, // seconds — skip quick app switches
restoreScreen: true,
})(App);| Option | What it does |
|---|---|
minimumBackgroundDuration | Seconds the app must have been in the background before ON_NEXT_RESUME or ON_NEXT_SUSPEND restart it. Default 0 — the same as before. |
restoreScreen | Wrapper only. Loads the saved screen state before your app renders; report it with codePush.trackScreenState and read codePush.restoredScreenState. |
Not using React Navigation, or calling CodePush.sync() yourself? Save any JSON just before a CodePush restart and read it back after:
codePush.setRestartStateProvider(() => ({ screen: currentScreen, orderId }));
// After a CodePush restart: the saved state, once. Null after a normal start or if the update rolled back.
const saved = await codePush.getRestoredState();- Saved state only comes back after a restart CodePush did — never after a normal cold start — and it's thrown away if the update rolled back.
- If the user comes back before the restart has happened, nothing restarts in front of them: it waits for the next time the app goes to the background. The update also runs on the next cold start, as always.
- The restart happens about a second after the app leaves the foreground, or after
minimumBackgroundDuration. Keep that short: iOS lets a backgrounded app run for about 30 seconds and recent Android versions can freeze it sooner — if that happens first, the update runs on the next cold start instead. - If your app hands the user to another app and waits for a result (a payment app, the camera), set
minimumBackgroundDurationabove how long that usually takes.
Prefer a wrapper? export default codePush(App) does the same sync on start, and codePush(options)(App) takes the same options plus checkFrequency.
Check that it works#
- Build a release build:
npx react-native run-android --mode release, or the Release scheme in Xcode. - Release a small visible change:
codeonair-codepush release-react myapp-android -t 1.0. - Open the app, close it, open it again — the change should be there. The build's page in the dashboard shows the download and install.