npm install -g code-push-cli
打开命令提示符
code-push register
code-push login
打开命令提示符
code-push login http://xxx.xxx.xxx.xxx:3000
在项目目录下执行下面命令
//注册Android
code-push app add 应用名 android react-native
//注册iOS
code-push app add 应用名 ios react-native
执行命令后会返回一套deployment key,环境分为Production和Staging,把该key在客户端中集成
npm install --save react-native-code-push
回到项目目录下,进行关联
react-native link react-native-code-push
此命令将会自动在anroid文件中添加好设置。
终端会提示让输入deployment key,只需将deployment Staging key输入即可
code-push deployment ls appName -k
code-push app list
apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"
buildTypes {
release {
buildConfigField "String", "codepushkey", '"填写Production key"'
}
debug {
buildConfigField "String", "codepushkey", '"填写Staging key"'
}
}
dependencies {
compile project(':react-native-code-push')
}
修改版本号,把versionName属性改成 三位,比如要修改成1.0.0
versionName "1.0.1"
include ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
@Override
protected String getJSBundleFile() {
return CodePush.getJSBundleFile();
}
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new CodePush(BuildConfig.codepushkey, MainApplication.this, BuildConfig.DEBUG,"http://192.168.0.5:8080")
);
}
执行下面命令打包,打包后会自动把文件上传到codepush服务器上
code-push release-react 注册的应用名 android --t 1.0.1 --dev false --d Staging --des"一些小修改" --m true
检查更新的位置根据项目来定
codePush.checkForUpdate().then((update) => {
if (update) {
codePush.sync(
{
ignoreFailedUpdates : false,
mandatoryInstallMode: codePush.InstallMode.IMMEDIATE,
},
(status) => {
switch (status) {
case codePush.SyncStatus.CHECKING_FOR_UPDATE:
this.setState({ syncMessage: "检查更新!" });
break;
case codePush.SyncStatus.DOWNLOADING_PACKAGE:
this.setState({ syncMessage: "正常下载安装包!" });
break;
case codePush.SyncStatus.AWAITING_USER_ACTION:
this.setState({ syncMessage: "等待用户操作!" });
break;
case codePush.SyncStatus.INSTALLING_UPDATE:
this.setState({ syncMessage: "正在安装...,请等待!" });
break;
case codePush.SyncStatus.UP_TO_DATE:
this.setState({ syncMessage: "已经是最新版本了!", progress: false });
break;
case codePush.SyncStatus.UPDATE_IGNORED:
this.setState({ syncMessage: "更新取消!", progress: false });
break;
case codePush.SyncStatus.UPDATE_INSTALLED:{
this.setState({ syncMessage: "新的版本已经安装完成,请重新登录!", progress: false });
codePush.restartApp();
break;
}
case codePush.SyncStatus.UNKNOWN_ERROR:
this.setState({ syncMessage: "更新发生错误,请重新启动程序!", progress: false });
break;
}
},
(progress) => {
this.setState({ progress });
}
);
}
else {
console.log('已经是最新版本');
this.init();
}
})