Skip to content

Instantly share code, notes, and snippets.

@devzarghami
Last active June 1, 2020 09:08
Show Gist options
  • Save devzarghami/2c69af835491d09543cbbbe46ce66e80 to your computer and use it in GitHub Desktop.
Save devzarghami/2c69af835491d09543cbbbe46ce66e80 to your computer and use it in GitHub Desktop.

Android & Pwa AppUpdate

cordova android AppUpdate

Required Cordova plugins

cordova-plugin-app-update

cordova-plugin-appversion

on mounted Android app called updateAndroidApp()

updateAndroidApp() {
   if (typeof AppVersion == 'undefined')
      return false

   var versionCode = AppVersion.build
   var updateUrl = 'https://raw.githubusercontent.com/vaenow/cordova-plugin-app-update-demo/master/remote_server_apk/version.xml';
   window.AppUpdate.checkAppUpdate(onSuccess, onFail, updateUrl, {
        'skipPromptDialog': false,
        'skipProgressDialog': false
   });

   function onFail() {
       //alert('fail', JSON.stringify(arguments), arguments);
   }
    function onSuccess() {
      //alert('success', JSON.stringify(arguments), arguments);
    }
}

Put the following file on the server

version.xml

<update>
	<version>10012</version>
	<name>monibapp</name>
	<url>https://raw.githubusercontent.com/vaenow/cordova-plugin-app-update-demo/master/remote_server_apk/android-debug-302048.apk</url>
</update>

Pwa AppUpdate

workbox config in nuxt.config.js

https://developers.google.com/web/tools/workbox/modules/workbox-strategies https://pwa.nuxtjs.org/modules/workbox.html

pwa:{
   workbox: {
   // cacheId: '1235',
   offlineStrategy: 'NetworkFirst'
   }
}

on mounted Pwa App called updateCheck()

async updateCheck() {
      const workbox = await window.$workbox;
      if (workbox) {
          workbox.precaching.addPlugins([
               new workbox.broadcastUpdate.Plugin('precache-channel')
          ])
      const updateChannel = new BroadcastChannel('precache-channel');
      updateChannel.addEventListener('message', event => {
          if (confirm(`بروز رسانی جدیدی در دسترس قرار گرفته است! برای بروزرسانی تایید کنید.`)) {
               window.location.reload();
          }
      });
    }
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment