Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ For Android, this implements the [Play Store In-App Update][playlib] system.
> Use version 1.x if you are building without AndroidX enabled.


## Custom JS Popup Support (Android)
Starting from version `3.1.0`, this plugin allows you to **replace the default Snackbar update prompt**
with your own JavaScript popup logic.

This is useful if you want to:
- Use your app's existing UI components instead of Material Snackbar
- Localize the update prompt dynamically
- Show a modal dialog instead of a banner

### How it works
The plugin now exposes a `UpdateNotifier.onReady(callback)` function.
When the update is downloaded and ready to install, your callback is triggered from JavaScript.

From your callback, you can:
- Display a custom UI (e.g., alert, modal, toast)
- Call `UpdateNotifier.completeUpdate()` to trigger installation

---

### Example Usage
```javascript
document.addEventListener('deviceready', function () {
UpdateNotifier.onUpdateReady(function () {
console.log("Update downloaded and ready!");

// Your custom popup
if (confirm("A new version is available. Install now?")) {
UpdateNotifier.completeUpdate();
}
});
});
```

Installation
------------

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-update-notifier",
"version": "3.0.0",
"version": "3.1.0",
"author": "Ayogo Health Inc. <info@ayogo.com>",
"contributors": [
"Darryl Pogue <darryl@dpogue.ca>",
Expand Down
10 changes: 8 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-update-notifier" version="3.0.0">
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-update-notifier" version="3.1.0">
<name>cordova-plugin-update-notifier</name>
<description>Cordova plugin for showing a notification for app updates.</description>
<keywords>cordova,ios,android</keywords>
Expand All @@ -27,6 +27,10 @@ limitations under the License.
<engine name="cordova-android" version=">= 9.0.0" />
</engines>

<js-module src="www/UpdateNotifier.js" name="UpdateNotifier">
<clobbers target="UpdateNotifier" />
</js-module>

<platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="UpdateNotifier">
Expand All @@ -40,7 +44,9 @@ limitations under the License.
<source-file src="src/ios/UpdateNotifierPlugin.swift" />

<podspec>
<config></config>
<config>
<source url="https://cdn.cocoapods.org/"/>
</config>
<pods use-frameworks="true">
<pod name="Siren" />
</pods>
Expand Down
36 changes: 33 additions & 3 deletions src/android/UpdateNotifierPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import com.google.android.material.snackbar.Snackbar;
import android.view.View;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;

import com.google.android.play.core.appupdate.AppUpdateInfo;
import com.google.android.play.core.appupdate.AppUpdateManager;
Expand All @@ -46,6 +49,7 @@ public class UpdateNotifierPlugin extends CordovaPlugin {
private final String TAG = "UpdateNotifierPlugin";
private static final Integer RC_APP_UPDATE = 577;

private CallbackContext onUpdateReadyCallback = null;

/**
* Called after plugin construction and fields have been initialized.
Expand All @@ -55,6 +59,23 @@ public void pluginInitialize() {
LOG.i(TAG, "Initializing");
}

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
if ("onUpdateReady".equals(action)) {
this.onUpdateReadyCallback = callbackContext;
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
return true;
} else if ("completeUpdate".equals(action)) {
if (mAppUpdateManager != null) {
mAppUpdateManager.completeUpdate();
}
callbackContext.success();
return true;
}
return false;
}

/**
* Called when the activity is becoming visible to the user.
Expand All @@ -65,7 +86,7 @@ public void onStart() {
@Override
public void onStateUpdate(InstallState state) {
if (state.installStatus() == InstallStatus.DOWNLOADED){
popupSnackbarForCompleteUpdate();
popupForCompleteUpdate();
} else if (state.installStatus() == InstallStatus.INSTALLED) {
if (mAppUpdateManager != null){
mAppUpdateManager.unregisterListener(mInstallListener);
Expand Down Expand Up @@ -104,7 +125,7 @@ public void onSuccess(AppUpdateInfo appUpdateInfo) {
e.printStackTrace();
}
} else if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
popupSnackbarForCompleteUpdate();
popupForCompleteUpdate();
} else {
LOG.e(TAG, "getAppUpdateInfo: Unhandled case");
}
Expand Down Expand Up @@ -140,10 +161,19 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
}
}

private void popupSnackbarForCompleteUpdate() {
private void popupForCompleteUpdate() {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {

if (onUpdateReadyCallback != null) {
PluginResult result = new PluginResult(PluginResult.Status.OK, true);
result.setKeepCallback(true);
onUpdateReadyCallback.sendPluginResult(result);
return;
}
LOG.w(TAG, "Update ready, but no JS callback was registered.");

Activity activity = cordova.getActivity();

int descID = activity.getResources().getIdentifier("app_update_ready", "string", activity.getPackageName());
Expand Down
41 changes: 41 additions & 0 deletions www/UpdateNotifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var exec = require('cordova/exec');

var UpdateNotifier = {
/**
* Registers a callback that will be triggered when the update is downloaded.
* @param {Function} callback - Function to execute when the update is ready
*/
onUpdateReady: function (callback) {
exec(
function () {
if (typeof callback === 'function') {
callback();
}
},
function (err) {
console.error("UpdateNotifier onUpdateReady error", err);
},
"UpdateNotifier",
"onUpdateReady",
[]
);
},

/**
* Completes the update installation
* (should be called in response to onUpdateReady)
*/
completeUpdate: function () {
exec(
null,
function (err) {
console.error("UpdateNotifier completeUpdate error", err);
},
"UpdateNotifier",
"completeUpdate",
[]
);
}
};

module.exports = UpdateNotifier;