in-app-updates-capacitor
Version:
Enables In App Updates on Android using the official APIs.
146 lines (106 loc) • 4.1 kB
Markdown
# in-app-updates-capacitor
Enables In App Updates on Android using the official Android APIs.
This plugin integrates the official Android APIs to perform in app updates that were released in 2019: https://developer.android.com/guide/app-bundle/in-app-updates
<br/>
<p float="left">
<img src="https://i.imgur.com/4KtxDDF.png" alt="drawing" width="300px"/>
<img src="https://i.imgur.com/7mJVfks.png" alt="drawing" width="300px"/>
</p>
## Installation
```bash
npm i in-app-updates-capacitor
npx cap sync
```
### Android
Register the plugin in your main activity:
```java
import com.dwlrathod.inappupdates.InAppUpdatesPlugin;
public class MainActivity extends BridgeActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initializes the Bridge
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
// Additional plugins you've installed go here
// Ex: add(TotallyAwesomePlugin.class);
add(InAppUpdatesPlugin.class);
}});
}
}
```
### iOS
iOS does not offer such functionality. If you call the methods above on an iOS device you'll run into a not-implemented exception.
### Browser
If you call the methods above on a Browser you'll run into a not-implemented exception.
## Usage
#### checkForUpdate()
Requests the update availability for the current app. This method need to call before calling `startFlexibleUpdate` or `performImmediateUpdate`.
```
import { Plugins } from "@capacitor/core";
const { InAppUpdatesPlugin } = Plugins;
import { AppUpdateInfo } from "in-app-updates";
InAppUpdatesPlugin.checkForUpdate().then(
(updateInfo: AppUpdateInfo) => {
console.log(updateInfo);
},
(error: any) => {
console.error("error: " + error);
}
);
```
`AppUpdateInfo` interface:
- `updateAvailable: boolean;`: Returns whether an update is available for the app.
- `immediateUpdateAllowed: boolean;`: Returns whether the immediate is allowed.
- `flexibleUpdateAllowed: boolean;` : Returns whether the flexible is allowed.
- `availableUpdateVersionCode: number;` : If an update is available or in progress, this method returns the version code of the update.
- `availableUpdatePriority: number;` : Returns the in -app update priority for this update, as defined by the developer in the Google Play Developer API.
#### startFlexibleUpdate()
Shows flexible update dialog. User will choose weather update or not.
```
InAppUpdatesPlugin.startFlexibleUpdate().then(
() => {
// Update downloaded complete and now you can start installing it.
// completeFlexibleUpdate()
},
(error: any) => {
// User select No Thanks from update dialog.
// error : user_canceled, no_update_found
}
);
```
`flexibleUpdateStateChange` listener will listen the status of downloading/downloaded update. You need to add the listener before calling `startFlexibleUpdate`.
```
import { FlexibleUpdateState } from "in-app-updates";
InAppUpdatesPlugin.addListener("flexibleUpdateStateChange", (info: FlexibleUpdateState) => {
console.log("flexibleUpdateStateChange was fired ", info.status);
if (info.status) {
this.appendEcho("status: " + info.status);
this.appendEcho("bytesDownloaded: " + info.bytesDownloaded);
this.appendEcho("totalBytesToDownload: " + info.totalBytesToDownload);
}
});
```
#### completeFlexibleUpdate()
For a flexible update flow, triggers the completion of the update. This method will only work with Flexible update and after update downloaded successfully.
```
InAppUpdatesPlugin.completeFlexibleUpdate().then(
() => {
// New update installation started succefully.
},
(error: any) => {
// error
}
);
```
#### performImmediateUpdate()
Performs an immediate update.
```
InAppUpdatesPlugin.performImmediateUpdate().then(
() => {
// Immediate update started
},
(error: any) => {
// error : user_canceled, no_update_found
}
);
```