# codePushDemo **Repository Path**: conan_ma/codePushDemo ## Basic Information - **Project Name**: codePushDemo - **Description**: 公开公开公开公开公开公开公开公开公开 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-03-28 - **Last Updated**: 2024-10-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 1. 整个 app 被 codepush 包裹 用于强制更新自动调用 # 2. 结合对用户个别升级计划,通常包含两层请求 自己接口判断是否升级(拿当前内部自定义 version 版本号)+codepush 接口请求,但此操作必须要在登录之后方可运行,如果不考虑用户个别升级计划可以直接在启动的时候直接调用版本更新,对版本进行升级操作 ## 2.1 用户个别升级计划模拟请求 ``` post({verison:,userId}).then(res=>{ if(res.needUpdate == true){ codePush.sync({ **** }) } }) ``` ## 2.2 直接请求codepush ``` codePush.sync({ **** }) ``` # 3. 非强制更新弹窗内容设定 ## 3.1 codePush内置的弹框 ``` CodePush.sync( { //安装模式 //ON_NEXT_RESUME 下次恢复到前台时 //ON_NEXT_RESTART 下一次重启时 //IMMEDIATE 马上更新 mandatoryInstallMode : CodePush.InstallMode.IMMEDIATE , deploymentKey: 'iOS平台Key,部署环境(Production/Staging)', //对话框 updateDialog : { //是否显示更新描述 appendReleaseDescription : true , //更新描述的前缀。 默认为"Description" descriptionPrefix : "更新内容:" , //强制更新按钮文字,默认为continue mandatoryContinueButtonLabel : "立即更新" , //强制更新时的信息. 默认为"An update is available that must be installed." mandatoryUpdateMessage : "必须更新后才能使用" , //非强制更新时,按钮文字,默认为"ignore" optionalIgnoreButtonLabel : '稍后' , //非强制更新时,确认按钮文字. 默认为"Install" optionalInstallButtonLabel : '后台更新' , //非强制更新时,检查到更新的消息文本 optionalUpdateMessage : '有新版本了,是否更新?' , //Alert窗口的标题 title : '更新提示' } } ); ``` ## 3.2 用户自己设定弹窗样式(需要用到两个方法) ``` CodePush.checkForUpdate(CODE_PUSH_KEY).then((update) => { console.log('-------' + update) if (!update) { // 实际可以不弹框 Toast.showLongSuccess('已是最新版本!') } else { showAlertFunc(); } }) ``` ``` CodePush.sync( {deploymentKey: CODE_PUSH_KEY, updateDialog: {}, installMode: CodePush.InstallMode.IMMEDIATE}, this.codePushStatusDidChange.bind(this), this.codePushDownloadDidProgress.bind(this) ) switch(syncStatus) { case CodePush.SyncStatus.CHECKING_FOR_UPDATE: this.syncMessage = 'Checking for update' break; case CodePush.SyncStatus.DOWNLOADING_PACKAGE: this.syncMessage = 'Downloading package' break; case CodePush.SyncStatus.AWAITING_USER_ACTION: this.syncMessage = 'Awaiting user action' break; case CodePush.SyncStatus.INSTALLING_UPDATE: this.syncMessage = 'Installing update' break; case CodePush.SyncStatus.UP_TO_DATE: this.syncMessage = 'App up to date.' break; case CodePush.SyncStatus.UPDATE_IGNORED: this.syncMessage = 'Update cancelled by user' break; case CodePush.SyncStatus.UPDATE_INSTALLED: this.syncMessage = 'Update installed and will be applied on restart.' break; case CodePush.SyncStatus.UNKNOWN_ERROR: this.syncMessage = 'An unknown error occurred' Toast.showError('更新出错,请重启应用!') this.setState({modalVisible: false}) break; } ``` # 正确使用code-push热更新 - 苹果App允许使用热更新[Apple's developer agreement](https://developer.apple.com/programs/ios/information/iOS_Program_Information_4_3_15.pdf), 为了不影响用户体验,规定必须使用静默更新。 Google Play不能使用静默更新,必须弹框告知用户App有更新。中国的android市场必须采用静默更新(如果弹框提示,App会被“请上传最新版本的二进制应用包”原因驳回)。 - react-native 不同平台bundle包不一样,在使用code-push-server的时候必须创建不同的应用来区分(eg. CodePushDemo-ios 和 CodePushDemo-android) - react-native-code-push只更新资源文件,不会更新java和Objective C,所以npm升级依赖包版本的时候,如果依赖包使用的本地化实现, 这时候必须更改应用版本号(ios修改Info.plist中的CFBundleShortVersionString, android修改build.gradle中的versionName), 然后重新编译app发布到应用商店。 - 推荐使用code-push release-react 命令发布应用,该命令合并了打包和发布命令(eg. code-push release-react CodePushDemo-ios ios -d Production) - 每次向App Store提交新的版本时,也应该基于该提交版本同时向code-push-server发布一个初始版本。(因为后面每次向code-push-server发布版本时,code-puse-server都会和初始版本比较,生成补丁版本) # 安卓非强制更新目前有问题 主要是没有sync 如果是下次启动生效没有调用notifyApplicationReady方法 目前需要修改react-native-code-push/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java下面的305行 判断if(updateIsLoading) 这个方法写死false 可能会影响回滚