参考网址:
http://222xiaohuan.iteye.com/blog/2347617
https://github.com/lisong/code-push-server/issues
步骤一: 将code-push-server下载下来,并安装依赖
node git clone https://github.com/lisong/code-push-server.git
cd code-push-server
npm i
步骤二: 初始化mysql数据库
node ./bin/db init --dbhost localhost --dbuser root —dbpassword
步骤三: 修改 config.js 文件 , 路径在 code-push-server/config/config.js
// Config for database, only support mysql.
db: {
username: process.env.RDS_USERNAME || "root",
password: process.env.RDS_PASSWORD || "123456", // 你的MySQL访问密码,如果没有就null
database: process.env.DATA_BASE || "codepush",
host: process.env.RDS_HOST || "127.0.0.1",
port: process.env.RDS_PORT || 3306,
dialect: "mysql",
logging: false
},
// Config for local storage when storageType value is "local".
local: {
// Binary files storage dir, Do not use tmpdir and it's public download dir.
// storageDir: process.env.STORAGE_DIR || "/Users/tablee/workspaces/storage",//需要你自己创建一个文件路径,把你的路径填上去
storageDir: "E:/storage" // 你的文件路径
// Binary files download host address which Code Push Server listen to. the files storage in storageDir.
// downloadUrl: process.env.LOCAL_DOWNLOAD_URL || "http://localhost:3000/download",
downloadUrl: "http://你的ip:3000/download",
// public static download spacename.
// public: process.env.PUBLIC || '/download'
public: '/download'
},
jwt: {
// Recommended: 63 random alpha-numeric characters
// Generate using: https://www.grc.com/passwords.htm
tokenSecret: 'ItFp6KHLXMHhAHRUvjCiyNNOfimZQmT7top3iHQakrb3Q1SdEiY3nol4nijzmpL' }, // 去 https://www.grc.com/passwords.htm 这里获取
common: {
/*
* tryLoginTimes is control login error times to avoid force attack.
* if value is 0, no limit for login auth, it may not safe for account. when it's a number, it means you can
* try that times today. but it need config redis server.
*/
tryLoginTimes: 0,
// CodePush Web(https://github.com/lisong/code-push-web) login address.
//codePushWebUrl: "http://localhost:3001/login",
// create patch updates's number. default value is 3
diffNums: 3,
// data dir for caclulate diff files. it's optimization.
// dataDir: process.env.DATA_DIR || "/Users/tablee/workspaces/data",,//需要你自己创建一个文件路径,把你的路径填上去
dataDir: "E:/storage" // 你的文件路径
// storageType which is your binary package files store. options value is ("local" | "qiniu" | "s3")
storageType: process.env.STORAGE_TYPE || "local",
// options value is (true | false), when it's true, it will cache updateCheck results in redis.
updateCheckCache: false
},
步骤四: 启动code-push-server
node ./bin/www
步骤五: 安装 code-push-cli
npm install code-push-cli@latest -g
code-push login http://127.0.0.1:3000 #会打开浏览器 帐号:admin 密码:123456 复制得到的token 粘贴到命令行回车
步骤六: 使用code-push添加 android app
添加Android App.pngstrings.xml MainApplication.java将我们得到的Production 的Deployment Key 填入你rn项目中,如下图
步骤七:使用code-push添加 ios app
code-push add app test-ios ios react-native
会和步骤六一样 , 将Deployment Key 填入 info.plist 中,如下图
Info.plist步骤八: 在你的rn项目中安装 react-native-code-push
npm install react-native-code-push@latest --save
react-native link react-native-code-push
⚠️ link 失败的参考 这篇文章 React Native CodePush实践小结
然后在你rn项目的入口文件中添加一下代码
import React, { Component } from 'react'
import {
View,
} from 'react-native'
import CodePush from 'react-native-code-push'
export default class Home extends Component {
constructor(props) {
super(props)
}
...
codePushDownloadDidProgress(progress) {
this.setState({ progress: progress.receivedBytes / progress.totalBytes })
}
codePushStatusDidChange(syncStatus) {
switch (syncStatus) {
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: "Installing update." });
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 });
break;
case CodePush.SyncStatus.UNKNOWN_ERROR:
// this.setState({ syncMessage: "An unknown error occurred.", progress: false });
break;
}
}
componentDidMount() {
CodePush.sync(
{
installMode: CodePush.InstallMode.IMMEDIATE,
updateDialog: {
optionalIgnoreButtonLabel: '稍后',
optionalInstallButtonLabel: '立即更新',
optionalUpdateMessage: '有新版本了,是否更新?',
title: '更新提示'
}
},
this.codePushStatusDidChange.bind(this),
this.codePushDownloadDidProgress.bind(this)
)
}
....
}
步骤九: 发布更新包
code-push release-react test-android android -d Production
code-push release-react test-ios ios -d Production
再次打开App就会看到弹框提示是否需要更新
网友评论