关于ionic的非二进制资源的云端更新

所谓的云端升级,就是热分布,ionic属于cordova项目,html,js,css,图片字体等资源能够经过应用自身从服务器下载最新版本并安装,这样的好处是下载量很是小,安装接近无缝,对最终用户无干扰或极少干扰,不知不觉中系统就更新了。固然这里这种发布只是针对前面说的几类资源的更新才有效,若是cordova插件更新,则仍是须要发行二进制版本,进行常规更新。css


方案一览(From https://github.com/gf-rd/blog/issues/8)

Ionic deployhtml

http://docs.ionic.io/docs/deploy-from-scratch
默认的实现是用ionic.io本身的服务器,固然也能够换成本身的服务器来拉取新资源包android

微软的code pushios

http://microsoft.github.io/code-push/index.html#getting_started
比起ionic 提供的deploy服务好很多。最重要是资源包是放在azure国内无压力(可更换,其次支持强制和推荐更新(可配给用户的提示更新文案changelog,支持不一样的channel(dev,staging,production等。git

cordova-app-loadergithub

https://github.com/markmarijnissen/cordova-app-loadernpm


综合考虑后,决定选择 code push方案服务器

 

codepush实验笔记

安装code-push-cli

$ sudo npm install -g code-push -dsession

注册code-push

$ code-push registerapp

A browser is being launched to authenticate your account. Follow the instructions it displays to complete your registration.

Enter your access key: xxxxxxxxxxxxx

Successfully logged-in. Your session file was written to /Users/kongzong/.code-push.config. You can run the code-push logout command at any time to delete this file and terminate your session.

新建code-push 应用

$ code-push app add myapp_android

Successfully added the "myapp_android" app, along with the following default deployments:

┌────────────┬──────────────────────────┐

│ Name       │ Deployment Key                        │

├────────────┼──────────────────────────┤

│ Production │ ********************************│

├────────────┼──────────────────────────┤

│ Staging    │ ********************************* │

└────────────┴──────────────────────────┘

建议android,ios分别建立两个应用,一个myapp_android,一个myapp_ios.

安装cordova插件

$cordova plugin add cordova-plugin-code-push

修改config.xml,填入deployment-key

<platform name="android">

    <preference name="CodePushDeploymentKey" value="YOUR-ANDROID-DEPLOYMENT-KEY" />

</platform>

<platform name="ios">

    <preference name="CodePushDeploymentKey" value="YOUR-IOS-DEPLOYMENT-KEY" />

</platform>

此处我使用Staging key,由于上传代码命令缺省用的就是Staging。

另外还要保证<access origin="*" /> 

另另外还要保证安装了whitelist插件,这个目前已是ionic的标配了。

 

app.js里修改代码,加在run方法的 ionicPlatform.read里

$ionicPlatform.ready(function () {
//其余代码
window.codePush.sync(
  function (syncStatus) {
    switch (syncStatus) {
      // Result (final) statuses
      case SyncStatus.UP_TO_DATE:
        navigator.notification.alert("一个更新被成功从云端安装。", null, '提示', '我知道了');("一个更新被成功从云端安装。", null, '提示', '我知道了');
        break;
      case SyncStatus.ERROR:
        navigator.notification.alert("云端更新出现错误。", null, '提示', '我知道了');
        break;     
    }
  },
  {
    updateDialog: false, installMode: InstallMode.IMMEDIATE  
  }
);
//其余代码
})

这里按照个人设计,基本静默安装,只在更新成功后有一个提示。

另外此处提示发现只能使用 navigator.notification.alert,试过 toast及ionicPopup都报错,好在 alert 提示也足够了。

使用code push上传更新代码:

$ sudo code-push release-cordova toc_android android

这将会把对应的www目录上传到code-push服务器。

从新进入你的app,就会自动下载更新,更新安装完成后有一个alert提示,点肯定后,你的app就更新到最新啦,是否是有点黑科技的感受。

 

以上只是最基本的运用,可是基本知足需求,更多细节的配置能够参考官方文档

 

https://www.npmjs.com/package/cordova-plugin-code-push#installmode

http://microsoft.github.io/code-push/docs/getting-started.html

 

<<EOF>>

相关文章
相关标签/搜索