Cordova热更新cordova-plugin-code-push

1. 搭建自己的 code-push-server 以及安装 CodePush CLI 客户端

传送门:windows服务器搭建code-push-server

登录自己的code-push-server服务器,创建提供 Cordova 热更新的应用。

Andoird: code-push add app <应用名-android> android cordova
IOS : code-push add app <应用名-ios> ios cordova

Cordova热更新cordova-plugin-code-push_第1张图片

2. 配置 Cordova 项目

创建完Cordova项目后,项目中添加:cordova-plugin-code-push 插件。

index.html 中加入

<meta http-equiv="Content-Security-Policy"
      content="default-src https://codepush.azurewebsites.net ${code-push-server域名} 'self' data: gap: cdvfile: https://ssl.gstatic.com ${code-push-server域名} 'unsafe-eval' 'unsafe-inline' https: http: wss: ws:; style-src 'self' 'unsafe-inline'; img-src * filesystem: cdvfile: data:; media-src * blob:"/>

将代码中的 ${code-push-server域名} 替换成自己的刚刚搭建的 code-push-server 域名或者ip,不用带端口号。(如:http://192.168.0.245 或者 http://www.xx.xx)

config.xml 中添加

    <platform name="android">
        ...
        <preference name="CodePushDeploymentKey" value="${Deployment Key}" />
        <preference name="CodePushServerUrl" value="${code-push-server域名}" />
    platform>
    <platform name="ios">
        ...
        <preference name="CodePushDeploymentKey" value="${Deployment Key}" />
        <preference name="CodePushServerUrl" value="${code-push-server域名}" />
    platform>

${Deployment Key} :替换成上面创建成功的 Deployment Key 。打包时添加 Production 参数就替换 ProductionDeployment Key
Cordova热更新cordova-plugin-code-push_第2张图片
${code-push-server域名} 替换成自己的刚刚搭建的 code-push-server 域名或者ip,【带端口号】(如:http://192.168.0.245:3000/ 或者 http://www.xx.xx/ ) 一定要以 / 结尾

index.js 中代码:

var app = {
  initialize: function () {
    this.bindEvents();
  },
  bindEvents: function () {
    window.document.addEventListener('deviceready', this.onDeviceReady, false);
  },
  onDeviceReady: function () {
    app.notifyApplicationReadys();
    app.checkForUpdates();
  },
  notifyApplicationReadys: function () {
    var onNotifySucceeded = function () {
      console.log("通知成功。");
    };
    var onNotifyFailed = function (error) {
      console.log("通知失败:" + error);
    };
    window.codePush.notifyApplicationReady(onNotifySucceeded, onNotifyFailed);
  },
  checkForUpdates: function () {
    var onInstallSuccess = function () {
      console.log("安装成功。");
    };
    var onUpdateCheck = function (remotePackage) {
      if (!remotePackage) {
        console.log("该应用程序是最新的。");
      } else {
        // The hash of each previously reverted package is stored for later use.
        // This way, we avoid going into an infinite bad update/revert loop.
        if (!remotePackage.failedInstall) {
          console.log("有可用的更新远程包:" + JSON.stringify(remotePackage));
          console.log("CodePush更新可用。包哈希值:" + remotePackage.packageHash);
          remotePackage.download(onPackageDownloaded, app.onError, onProgress);
        } else {
          console.log("之前尝试了可用的更新,但失败了。");
        }
      }
    };
    var onPackageDownloaded = function (localPackage) {
      console.log("包下载地址:" + localPackage.localPath);
      console.log("本地包 appVersion: " + localPackage.appVersion);
      console.log("本地包 description: " + localPackage.description);
      console.log("本地包 failedInstall: " + localPackage.failedInstall);
      console.log("本地包 isFirstRun: " + localPackage.isFirstRun);
      console.log("本地包 isMandatory: " + localPackage.isMandatory);
      console.log("本地包 packageSize: " + localPackage.packageSize);
      // InstallMode.IMMEDIATE: 立即更新APP
      // InstallMode.ON_NEXT_RESTART: 到下一次启动应用时更新
      // InstallMode.ON_NEXT_RESUME: 当应用从后台返回时更新
      localPackage.install(onInstallSuccess, app.onError, {
        installMode: InstallMode.IMMEDIATE,
        mandatoryInstallMode: InstallMode.IMMEDIATE
      });
    };

    var onProgress = function (downloadProgress) {
      console.log("下载中: " + downloadProgress.receivedBytes + " 共: " + downloadProgress.totalBytes + " bytes.");
    };
    window.codePush.checkForUpdate(onUpdateCheck, app.onError);
  },
  onError: function (error) {
    console.log("更新发生错误: " + error);
  },
};
app.initialize();

3. 发布更新

code-push release-cordova <应用名称>-android android -m --des "更新描述" Staging   #默认是打包Staging环境的,正式环境打包方式如下
code-push release-cordova <应用名称>-android android -m --des "更新描述" Production  #打包正式环境

-m : 表示是否强制更新

code-push 还有很多常用命令

code-push login 登陆
code-push loout 注销
code-push access-key ls 列出登陆的token
code-push access-key rm 删除某个 access-key
code-push app add 在账号里面添加一个新的app
code-push app remove 或者 rm 在账号里移除一个app
code-push app rename 重命名一个存在app
code-push app list 或则 ls 列出账号下面的所有app
code-push deployment ls <应用名>-android -k 查看:<应用名>-android的deployment key

你可能感兴趣的:(Cordova)