capacitor-plugin-codepush v6.0.1
capacitor-plugin-codepush
web hot replace
server base on code-push-server
大版本跟@capacitor
v6.x support
Capacitor 6
Getting Started
With the Capacitor plugin installed, configure your app to use it via the following steps:
Add your deployment & code signing (optional) keys to the
capacitor.config.json
file, making sure to include the right key:"Plugins": { ... (other plugins) "CodePush": { "IOS_DEPLOY_KEY": "IOS_DEPLOYMENT_KEY", "IOS_PUBLIC_KEY": "APP_SECRET_KEY", "ANDROID_DEPLOY_KEY": "ANDROID_DEPLOYMENT_KEY", "ANDROID_PUBLIC_KEY": "APP_SECRET_KEY", "SERVER_URL": "https://codepush.appcenter.ms/" } }
As a reminder, the deployment keys are generated for you when you created your CodePush app via the CLI. If you need to retrieve them, you can simply run
appcenter codepush deployment list --app <ownerName>/<appName> --displayKeys
, and grab the key for the specific deployment you want to use (e.g.Staging
,Production
). App IDs can be retrieved by runningappcenter apps list
.The public keys should be generated by you, as should the corresponding private keys.
# generate private RSA key and write it to private.pem file openssl genrsa -out private.pem # export public key from private.pem into public.pem openssl rsa -pubout -in private.pem -out public.pem
NOTE: You must create a separate CodePush app for iOS and Android, which is why the above sample illustrates declaring separate keys for Android and iOS. If you're only developing for a single platform, then you only need to specify the deployment key for either Android or iOS.
To ensure that your app can access the CodePush server on CSP-compliant platforms, add
https://codepush.appcenter.ms
to theContent-Security-Policy
meta
tag in yourindex.html
file:<meta http-equiv="Content-Security-Policy" content="default-src https://codepush.appcenter.ms 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *" />
Install
npm install capacitor-plugin-codepush
npx cap sync
import { CodePush } from 'capacitor-plugin-codepush';
import { Plugins, AppState } from '@capacitor/core';
CodePush.sync();
const { App } = Plugins;
App.addListener('appStateChange', (state: AppState) => {
// state.isActive contains the active state
if (state.isActive) {
codePush.sync();
}
});
// cli本地部署只支持到2.1.9
npm i -g code-push-cli@2.1.9
// 登录,获取key
code-push login https://code-push.xxx.cn/
//给app在热更新服务器上创建应用
code-push app add <appName> <os> <platform>
//删除应用
code-push app rm <appName>
//查看热更新服务器上有哪些应用
code-push app list
//发布应用
code-push release-cordova <appName> <platform> [options]
Options参数:
--deploymentName, -d ..指定部署的类型.默认"Staging",可以选择"Production"或其他 自定义类型
--description, --des ..添加描述
--mandatory, -m .......指定此版本是否为强制更新版本
例1:发布更新
code-push release-cordova ionic2_tabs_android android --des ""
例2:部署"Production"状态的更新,即生产环境的热更新部署使用这句命令
code-push release-cordova ionic2_tabs_android android -d "Production" --des ""
注意:一般生产环境的app是压缩过的,所以在发布正式环境热更新之前,先执行"ionic build --prod"压缩代码
例3:部署ios应用的更新
code-push release-cordova ionic2_tabs_ios ios --des ""
例4:添加-m参数强制更新,code-push插件从服务端下载完代码,会立即自动重启app
code-push release-cordova ionic2_tabs_android android -m --des ""
//查看部署状态
code-push deployment list <appName>
例1:
code-push deployment list ionic2_tabs_android
例2:查看部署状态及key值,忘记key就这样找
code-push deployment list ionic2_tabs_android -k
//清空部署记录
code-push deployment clear <appName> <deploymentName>
如:清空Staging状态的部署记录
code-push deployment clear ionic2_tabs_android Staging
//添加部署状态,默认只有"Staging"和"Production"两中状态
code-push deployment add <appName> [deploymentName]
//删除自定义的部署状态
code-push deployment rm <appName> <deploymentName>
API
getDeploymentKey()
getServerURL()
getPublicKey()
decodeSignature(...)
getBinaryHash()
getPackageHash(...)
notifyApplicationReady()
isFirstRun(...)
isPendingUpdate()
isFailedUpdate(...)
install(...)
reportFailed(...)
reportSucceeded(...)
restartApplication()
preInstall(...)
getAppVersion()
getNativeBuildTime()
unzip(...)
addListener('codePushStatus', ...)
- Interfaces
getDeploymentKey()
getDeploymentKey() => any
获取deployment key
Returns: any
getServerURL()
getServerURL() => any
获取服务器url
Returns: any
getPublicKey()
getPublicKey() => any
获取public key
Returns: any
decodeSignature(...)
decodeSignature(options: NativeDecodeSignatureOptions) => any
解密签名
Param | Type |
---|---|
options | NativeDecodeSignatureOptions |
Returns: any
getBinaryHash()
getBinaryHash() => any
Returns: any
getPackageHash(...)
getPackageHash(options: NativePathOptions) => any
Param | Type |
---|---|
options | NativePathOptions |
Returns: any
notifyApplicationReady()
notifyApplicationReady() => any
Returns: any
isFirstRun(...)
isFirstRun(options: NativeHashOptions) => any
Param | Type |
---|---|
options | NativeHashOptions |
Returns: any
isPendingUpdate()
isPendingUpdate() => any
Returns: any
isFailedUpdate(...)
isFailedUpdate(options: NativeHashOptions) => any
获取失败的更新
Param | Type |
---|---|
options | NativeHashOptions |
Returns: any
install(...)
install(options: NativeInstallOptions) => any
安装更新
Param | Type |
---|---|
options | NativeInstallOptions |
Returns: any
reportFailed(...)
reportFailed(options: NativeStatusReportOptions) => any
报告失败
Param | Type |
---|---|
options | NativeStatusReportOptions |
Returns: any
reportSucceeded(...)
reportSucceeded(options: NativeStatusReportOptions) => any
报告成功
Param | Type |
---|---|
options | NativeStatusReportOptions |
Returns: any
restartApplication()
restartApplication() => any
重启应用
Returns: any
preInstall(...)
preInstall(options: NativeInstallOptions) => any
安装前校验
Param | Type |
---|---|
options | NativeInstallOptions |
Returns: any
getAppVersion()
getAppVersion() => any
获取版本
Returns: any
getNativeBuildTime()
getNativeBuildTime() => any
获取构建时间
Returns: any
unzip(...)
unzip(options: NativeUnzipOptions) => any
解压文件
Param | Type |
---|---|
options | NativeUnzipOptions |
Returns: any
addListener('codePushStatus', ...)
addListener(eventName: "codePushStatus", listenerFunc: (info: any) => void) => void
codepush过程监听
Param | Type |
---|---|
eventName | 'codePushStatus' |
listenerFunc | (info: any) => void |
Interfaces
PluginCallResponse
Prop | Type |
---|---|
value | T |
NativeDecodeSignatureOptions
Prop | Type |
---|---|
publicKey | string |
signature | string |
NativePathOptions
Prop | Type |
---|---|
path | string |
NativeHashOptions
Prop | Type |
---|---|
packageHash | string |
NativeInstallOptions
Prop | Type |
---|---|
startLocation | string |
NativeStatusReportOptions
Prop | Type |
---|---|
statusReport | StatusReport |
StatusReport
Prop | Type |
---|---|
status | number |
label | string |
appVersion | string |
deploymentKey | string |
lastVersionLabelOrAppVersion | string |
lastVersionDeploymentKey | string |
NativeUnzipOptions
Prop | Type |
---|---|
zipFile | string |
targetDirectory | string |