环境初始化
-
全局安装 code-push-cli
npm install code-push-cli@latest -g
没有权限加入 sudo进行下载
-
将 react-native-code-push 安装到本地 react-native 项目
npm install --save react-native-code-push@latest react-native link react-native-code-push #提示输入配置请跳过(ignore)
登录服务器,在服务器创建对应的热更新项目
-
code-push login 服务器地址
将自动调出用浏览器,请在浏览器界面输入账户`admin`及密码`Yld2018` 按提示将web页面上的token填写到命令行中
code-push app add <appName> <os> <platform>
例:`code-push app add codePushDemo-ios ios react-native `
- 例:`code-push app add codePushDemo-andr oid android react-native `
- app名后请一定加上 `-ios或-android` ,以便于区分。
- 成功后返回 `Production(生产环境)` 与 `Staging(测试环境)` 两组`Production DeploymentKey`值,请注意保存与区分。
IOS 配置
-
编辑`Info.plist`文件,添加CodePushDeploymentKey和CodePushServerURL
-
CodePushDeploymentKey值设置为对应的Production DeploymentKey值。
-
CodePushServerURL值设置为code-push-server服务器地址 http://120.77.173.126:3000/
- 将默认版本号1.0改成三位1.0.0
js
<key>CodePushDeploymentKey</key>
<string>YourCodePushKey</string>
<key>CodePushServerURL</key>
<string>YourCodePushServerUrl</string>
(注:服务器地址是http 记得配置白名单)
Android 配置
-
编辑 MainApplication.java
import com.microsoft.codepush.react.CodePush;
###
@Override
protected String getJSMainModuleName() {
return "index";
}
@Override
protected String getJSBundleFile() {
return CodePush.getJSBundleFile();
}
###
-
修改string.xml文件
<resources>
<string name="app_name">Geek</string>
<string moduleConfig="true" name="CodePushDeploymentKey">CodePushDeploymentKey</string>
<string moduleConfig="true" name="CodePushDeploymentServer">CodePushDeploymentServer</string>
</resources>
-
null替换成对应的Production DeploymentKey值
-
YourCodePushServerUrl`值设置为code-push-server服务地址 http://120.77.173.126:3000/
-
将默认版本号1.0改成三位1.0.0
js
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new CodePush(
null,
getApplicationContext(),
BuildConfig.DEBUG,
"YourCodePushServerUrl"
)
);
}
推bundle包至服务器
-
code-push release-react <APP名称> <部署环境> -d <生产环境/测试环境>
例: `code-push release-react yiliandai-ios ios -d Production`
- 注:请无视报错`[Error] Could not parse response`
使用方式
- 在头部引入 import CodePush from "react-native-code-push"
-
于程序入口处的 `componentDidMount`加入以下代码
js
CodePush.sync({
installMode:CodePush.InstallMode.IMMEDIATE,
updateDialog: false
});
代码段解释
installMode
codePush.InstallMode.IMMEDIATE表示安装更新并立即重新启动应用程序
updateDialog
false表示不做任何弹窗提示