Keeping your app up-to-date on your users’ devices.
This feature works only on Android devices starting from API level 21 (Android 5.0).
Before going any further make sure that you:
- have published android application in Google Play Console.
- have turned on internal app sharing in Google Play Console.
- have your React Native project.
- Add package in to the project
yarn add https://github.com/kyivstarteam/react-native-in-app-update.git
A user experience that provides background download and installation with graceful state monitoring. This UX is appropriate when it’s acceptable for the user to use the app while downloading the update.
import { View, Text, Alert } from 'react-native';
import React, { useEffect } from 'react';
import InAppUpdate from '@kyivstarteam/react-native-in-app-update';
const inAppUpdate = new InAppUpdate({ updateType: InAppUpdate.appUpdateType.FLEXIBLE });
const onDownloadedUpdate = () => {
Alert.alert(
'An update has just been downloaded.',
'Want to restart the app?',
[
{ text: 'No', style: 'cancel' },
{ text: 'Yes', onPress: inAppUpdate.completeUpdate },
],
);
};
const checkAvailableUpdate = async () => {
const isUpdateAvailable = await inAppUpdate.isUpdateAvailable();
if (isUpdateAvailable) {
inAppUpdate.onFinishDownloadUpdate(onDownloadedUpdate);
await inAppUpdate.startUpdate();
}
};
const App = () => {
useEffect(() => {
checkAvailableUpdate();
}, []);
return (
<View>
<Text>App</Text>
</View>
);
};- If you want to check for downloading status, for example if the user closed the application until the download is complete. You can check this status by isUpdatedDownloaded method. See example below
import { View, Text, Alert } from 'react-native';
import React, { useEffect } from 'react';
import InAppUpdate from '@kyivstarteam/react-native-in-app-update';
const inAppUpdate = new InAppUpdate({ updateType: InAppUpdate.appUpdateType.FLEXIBLE });
const completeUpdate = () => {
Alert.alert(
'An update has just been downloaded.',
'Want to restart the app?',
[
{ text: 'No', style: 'cancel' },
{ text: 'Yes', onPress: inAppUpdate.completeUpdate },
],
);
};
const checkIsUpdateDownloaded = async () => {
const isUpdateDownloaded = await inAppUpdate.isUpdateDownloaded();
if (isUpdateDownloaded) {
completeUpdate();
}
};
const App = () => {
useEffect(() => {
checkIsUpdateDownloaded();
}, []);
return (
<View>
<Text>App</Text>
</View>
);
};A full screen user experience that requires the user to update and restart the app in order to continue using the app. This UX is best for cases where an update is critical for continued use of the app. After a user accepts an immediate update, Google Play handles the update installation and app restart.
import { View, Text } from 'react-native';
import React, { useEffect } from 'react';
import InAppUpdate from '@kyivstarteam/react-native-in-app-update';
const inAppUpdate = new InAppUpdate({ updateType: InAppUpdate.appUpdateType.IMMEDIATE });
const checkAvailableUpdateAndInstall = async () => {
const isUpdateAvailable = await inAppUpdate.isUpdateAvailable();
if (isUpdateAvailable) {
await inAppUpdate.startUpdate();
}
};
const App = () => {
useEffect(() => {
checkAvailableUpdateAndInstall();
}, []);
return (
<View>
<Text>App</Text>
</View>
);
}; {
updateType: AppUpdateType, // Update type, Flexible or Immediate
stalenessDays: number, // how much time should passed since the update has released
}Return true or false
inAppUpdate.isUpdateAvailable();Return true or false. If you start an immediate update, and during the process will close app.
You should check that update is not stalled. If its true. Just complete update.
inAppUpdate.isUpdatePaused();Function that initiate updating
inAppUpdate.startUpdate();Returning update status ('FAILED' | 'SUCCESS' | 'INACTIVE');
inAppUpdate.getUpdateStatus();Return true or false. This method using with flexible update.
inAppUpdate.isUpdateDownloaded();Function that start updating after downloading
inAppUpdate.completeUpdate();Listener will trigger when update downloading is finished.
inAppUpdate.onFinishDownloadUpdate(callback: () => void);