跳转到正文

函数与设置

GitHub

请参阅 Github README 了解更多信息。

可以通过以下选项配置 CapacitorUpdater:

属性类型描述默认自
appReadyTimeoutnumber配置原生插件在判定更新为“失败”前应等待的毫秒数。适用于 Android、iOS 和 Electron。10000 // (10 seconds)
responseTimeoutnumber配置原生插件在将 API 视为超时前应等待的毫秒数。适用于 Android、iOS 和 Electron。20000 // (20 seconds)
autoDeleteFailedboolean配置插件是否自动删除失败的 bundle。适用于 Android、iOS 和 Electron。true
autoDeletePreviousboolean配置插件是否在更新成功后自动删除之前的 bundle。适用于 Android、iOS 和 Electron。true
autoUpdateboolean | ‘off’ | ‘atBackground’ | ‘atInstall’ | ‘onLaunch’ | ‘always’ | ‘onlyDownload’配置插件如何通过更新服务器使用 Auto Update。true 等同于 “atBackground”;false 等同于 “off”。- off:禁用 Auto Update - atBackground:自动检查并下载,然后在应用移至后台时应用 - atInstall:仅在全新安装或原生应用更新后立即应用,否则使用 atBackground - onLaunch:启动时立即应用,否则在启动检查后使用 atBackground - always:每次 Auto Update 运行时立即应用 - onlyDownload:自动检查并下载,触发 updateAvailable,但绝不自动设置下一个 bundle。即时应用模式(“atInstall”、“onLaunch”、“always”)应使用 —delta 上传,以免更新拖慢用户体验。在 Android 和 iOS 上,这些模式需要 autoSplashscreen: true,并且 @capacitor/splash-screen 需配合 launchAutoHide: false。适用于 Android、iOS 和 Electron。“atBackground” // true is still accepted
resetWhenUpdateboolean当设备上安装了更新的 native app bundle 时,自动删除之前下载的 bundle。支持 Android、iOS 和 Electron。true
updateUrlstring配置发送更新检查请求的 URL / endpoint。支持 Android、iOS 和 Electron。https://plugin.capgo.app/updates
channelUrlstring配置用于 channel 操作的 URL / endpoint。支持 Android、iOS 和 Electron。https://plugin.capgo.app/channel_self
statsUrlstring配置发送更新统计数据的 URL / endpoint。支持 Android、iOS 和 Electron。设置为 "" 可禁用统计上报。https://plugin.capgo.app/stats
publicKeystring配置用于端到端 live update 加密(Version 2)的公钥。支持 Android、iOS 和 Electron。undefined6.2.0
versionstring配置应用的当前版本。这将用于首次更新请求。如果未设置,插件将从 native code 获取版本。支持 Android、iOS 和 Electron。undefined4.17.48
directUpdateboolean | ‘always’ | ‘atInstall’ | ‘onLaunch’已弃用。请改用 autoUpdate 字符串模式:“atInstall”、“onLaunch” 或 “always”。此选项仍受支持以兼容现有应用。即时应用(Instant apply)应使用 —delta 上传,以免更新拖慢用户体验。在 Android 和 iOS 上,这些模式需要 autoSplashscreen: true 以及 @capacitor/splash-screen 且 launchAutoHide: false。- false: 从不执行直接更新 - atInstall: 等同于 autoUpdate: “atInstall” - onLaunch: 等同于 autoUpdate: “onLaunch” - always: 等同于 autoUpdate: “always” - true: 为向后兼容,等同于 “always”。支持 Android、iOS 和 Electron。false5.1.0
autoSplashscreenboolean在使用即时应用模式(“atInstall”、“onLaunch” 或 “always”)时必填,包括已弃用的 directUpdate 值。必须安装并配置 @capacitor/splash-screen,且 launchAutoHide 设为 false。启用后,插件会在更新应用后或无需更新时隐藏启动画面。这消除了手动监听 appReady 事件并调用 SplashScreen.hide() 的需要。支持 Android 和 iOS。false7.6.0
periodCheckDelaynumber配置定期更新检查的延迟时间,单位为秒。支持 Android、iOS 和 Electron。不能小于 600 秒(10 分钟)。600 // (10 minutes)
localS3boolean配置 CLI 以使用本地服务器进行测试,或使用自托管的更新服务器。undefined4.17.48
localHoststring配置 CLI 以使用本地服务器进行测试,或使用自托管的更新服务器。undefined4.17.48
localWebHoststring配置 CLI 以使用本地服务器进行测试,或使用自托管的更新服务器。undefined4.17.48
localSupastring配置 CLI 以使用本地服务器进行测试,或使用自托管的更新服务器。undefined4.17.48
localSupaAnonstring配置 CLI 以使用本地服务器进行测试。undefined4.17.48
localApistring配置 CLI 以使用本地 api 进行测试。undefined6.3.3
localApiFilesstring配置 CLI 以使用本地文件 api 进行测试。undefined6.3.3
allowModifyUrlboolean允许插件从 JavaScript 端动态修改 updateUrl、statsUrl 和 channelUrl。false5.4.0
defaultChannelstring在配置中设置应用的默认 channel。区分大小写。此设置将覆盖云端设置的默认 channel,但仍会尊重云端所做的覆盖设置。undefined5.5.0
appIdstring在配置中设置应用的 app id。undefined6.0.0
keepUrlPathAfterReloadboolean配置插件以在重载后保留 URL 路径。警告:触发重载时,‘window.history’ 将被清空。false6.8.0
disableJSLoggingboolean禁用插件的 JavaScript 日志。如果设为 true,插件将不会向 JavaScript 控制台打印日志,仅保留原生日志。false7.3.0
shakeMenuboolean启用摇一摇手势以显示更新菜单,用于调试和测试false7.5.0

在 capacitor.config.json:

{
"plugins": {
"CapacitorUpdater": {
"appReadyTimeout": 1000 // (1 second),
"responseTimeout": 10 // (10 second),
"autoDeleteFailed": false,
"autoDeletePrevious": false,
"autoUpdate": "onlyDownload",
"resetWhenUpdate": false,
"updateUrl": https://example.com/api/auto_update,
"channelUrl": https://example.com/api/channel,
"statsUrl": https://example.com/api/stats,
"publicKey": undefined,
"version": undefined,
"directUpdate": undefined,
"autoSplashscreen": undefined,
"periodCheckDelay": undefined,
"localS3": undefined,
"localHost": undefined,
"localWebHost": undefined,
"localSupa": undefined,
"localSupaAnon": undefined,
"localApi": undefined,
"localApiFiles": undefined,
"allowModifyUrl": undefined,
"defaultChannel": undefined,
"appId": undefined,
"keepUrlPathAfterReload": undefined,
"disableJSLogging": undefined,
"shakeMenu": undefined
}
}
}

在 capacitor.config.ts:

import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
CapacitorUpdater: {
appReadyTimeout: 1000 // (1 second),
responseTimeout: 10 // (10 second),
autoDeleteFailed: false,
autoDeletePrevious: false,
autoUpdate: 'onlyDownload',
resetWhenUpdate: false,
updateUrl: https://example.com/api/auto_update,
channelUrl: https://example.com/api/channel,
statsUrl: https://example.com/api/stats,
publicKey: undefined,
version: undefined,
directUpdate: undefined,
autoSplashscreen: undefined,
periodCheckDelay: undefined,
localS3: undefined,
localHost: undefined,
localWebHost: undefined,
localSupa: undefined,
localSupaAnon: undefined,
localApi: undefined,
localApiFiles: undefined,
allowModifyUrl: undefined,
defaultChannel: undefined,
appId: undefined,
keepUrlPathAfterReload: undefined,
disableJSLogging: undefined,
shakeMenu: undefined,
},
},
};
export default config;
notifyAppReady() => Promise<AppReadyResult>

通知 Capacitor Updater 当前 bundle 运行正常(如果每次应用启动时未调用此方法,将触发回滚) 默认情况下,应在应用启动后的前 10 秒内调用此方法,否则将触发回滚。 可通过 {@link appReadyTimeout} 修改此行为

返回值: Promise<AppReadyResult>


setUpdateUrl(options: UpdateUrl) => Promise<void>

设置应用的 updateUrl,将用于检查更新。

参数类型描述
optionsUpdateUrl包含用于检查更新的 URL。

自: 5.4.0


setStatsUrl(options: StatsUrl) => Promise<void>

为应用设置 statsUrl,该 URL 将用于发送统计数据。传入空字符串将禁用统计收集。

参数类型描述
optionsStatsUrl包含用于发送统计数据的 URL。

自: 5.4.0


setChannelUrl(options: ChannelUrl) => Promise<void>

设置应用的 channelUrl,该值将用于设置频道。

参数类型描述
optionsChannelUrl包含用于设置频道的 URL。

自: 5.4.0


download(options: DownloadOptions) => Promise<BundleInfo>

从提供的 URL 下载新的 bundle。该 URL 应指向一个 zip 文件,其中包含文件,或者包含一个唯一 ID,里面带有你的所有文件。

参数类型描述
optionsDownloadOptions{@link DownloadOptions} 用于下载新的 bundle zip。

返回值: Promise<BundleInfo>


next(options: BundleId) => Promise<BundleInfo>

设置应用重新加载时要使用的下一个 bundle。

参数类型描述
optionsBundleId包含下次应用启动时要设置的下一个 Bundle 的 ID。{@link BundleInfo.id}

返回值: Promise<BundleInfo>


set(options: BundleId) => Promise<void>

设置当前 Bundle 并立即重新加载应用。

参数类型描述
optionsBundleId一个 {@link BundleId} 对象,包含要设为当前的新 bundle id。

delete(options: BundleId) => Promise<void>

从原生应用存储中删除指定的 bundle。配合 {@link list} 可获取已存储的 Bundle IDs。

参数类型描述
optionsBundleId一个 {@link BundleId} 包含要删除的 bundle 的 ID 的对象(注意,这是 bundle id,而不是版本名称)

list(options?: ListOptions | undefined) => Promise<BundleListResult>

获取应用中所有本地下载的 bundle

参数类型描述
optionsListOptions{@link ListOptions} 用于列出 bundle

返回值: Promise<BundleListResult>


reset(options?: ResetOptions | undefined) => Promise<void>

将应用重置为 builtin bundle(即发送至 Apple App Store / Google Play Store 的版本)或上一次成功加载的 bundle。

参数类型描述
optionsResetOptions包含 {@link ResetOptions.toLastSuccessful}, true 重置为内置 bundle 并 false 将重置为最后一次成功加载的 bundle。

current() => Promise<CurrentBundleResult>

获取当前 bundle,如果未设置则返回 builtin。currentNative 是设备上安装的原始 bundle

返回值: Promise<CurrentBundleResult>


reload() => Promise<void>

重新加载视图


setMultiDelay(options: MultiDelayConditions) => Promise<void>

设置一个 {@link DelayCondition} 数组,其中包含插件用于延迟更新的多个条件。 当所有条件均满足后,更新流程将像往常一样重新运行,因此更新将在应用进入后台或被杀死后安装。 对于 date 类型,值应为 ISO 8601 日期字符串。 对于 background 类型,值应为以毫秒为单位的数字。 对于 nativeVersion 类型,值应为版本号。 对于 kill 类型,值不会被使用。 该函数在 kill 选项上的行为不一致:它会在第一次杀死应用后触发更新,而不是像其他选项那样在下次进入后台时触发。此问题将在未来的主要版本中修复。

参数类型描述
optionsMultiDelayConditions包含 {@link MultiDelayConditions} 要设置的条件数组

自: 4.3.0


cancelDelay() => Promise<void>

取消 {@link DelayCondition},以立即处理更新。

自: 4.0.0


getLatest(options?: GetLatestOptions | undefined) => Promise<LatestVersion>

获取更新 URL 中可用的最新 bundle

参数类型
optionsGetLatestOptions

返回值: Promise<LatestVersion>

自: 4.0.0


setChannel(options: SetChannelOptions) => Promise<ChannelRes>

为此设备设置由插件管理的本地 channel。该 channel 必须已 allow_device_self_set 启用才能生效。

setChannel() 会向后端验证通道,然后把所选通道存储在设备本地。它不会创建或更新后端的 Device Override,因此设备不会在 Capgo dashboard 中显示为已覆盖。只有从 dashboard 或 Public API 创建的分配才会显示在 Device Override UI 中。

重要说明:

  • 不要在启动时用此方法设置通道。请改用 defaultChannel 在您的 Capacitor 配置中设置。
  • 此方法适用于应用就绪且用户已进行交互后(例如加入 Beta 计划)。
  • 公共通道不能自行分配。 如果通道被标记为 public,调用 setChannel() 会返回错误。要使用公共通道,请调用 unsetChannel() 作为替代 - 设备将自动回退到匹配的公共通道。
  • 使用 listChannels() 以查看哪些通道可用,以及它们是否允许自行分配。
参数类型描述
optionsSetChannelOptions是否将 {@link SetChannelOptions} 通道设置为

返回值: Promise<ChannelRes>

自: 4.7.0


unsetChannel(options: UnsetChannelOptions) => Promise<void>

取消设置此设备由插件管理的本地通道。此操作仅清除由 setChannel(); 它不会删除 Dashboard 或 Public API Device Override 记录。

调用此方法后,正常的 channel 优先级规则生效:现有的 Dashboard 或 Public API Device Override 仍然优先;否则,设备可以根据自身条件(平台、设备类型、build type)回退到匹配的 public/default channel。

适用于以下场景:

  • 您想将设备移回默认更新轨道
  • 您想使用 public channel(因为 public channel 无法通过 setChannel())
参数类型
optionsUnsetChannelOptions

自: 4.7.0


getChannel() => Promise<GetChannelRes>

获取此设备的 channel

返回值: Promise<GetChannelRes>

自: 4.8.0


listChannels() => Promise<ListChannelsResult>

列出该设备可用的所有 channel。返回与设备当前环境(平台、模拟器/真机、开发/生产构建)兼容,且为公开或允许自行分配的 channel。

结果中的每个 channel 包含:

  • public:如果 true,这是一个 默认 channel。你无法使用 setChannel()将其分配给自己。相反,如果你要移除你的 channel 分配,请使用 unsetChannel(),设备将自动从此公共通道接收更新。
  • allow_self_set:如果 true,这是一个 可自行分配的 Channel。您可以使用 setChannel().

返回值: Promise<ListChannelsResult>

自: 7.5.0


setCustomId(options: SetCustomIdOptions) => Promise<void>

为此设备设置自定义 ID

参数类型描述
optionsSetCustomIdOptions是 {@link SetCustomIdOptions} 要设置的 customId

自: 4.9.0


getBuiltinVersion() => Promise<BuiltinVersion>

获取原生应用版本;若在配置中已设置,则返回内置版本

返回值: Promise<BuiltinVersion>

自: 5.2.0


getDeviceId() => Promise<DeviceId>

获取用于识别设备的唯一 ID(发送至自动更新服务器)

返回值: Promise<DeviceId>


getPluginVersion() => Promise<PluginVersion>

获取原生 Capacitor Updater 插件版本(发送至自动更新服务器)

返回值: Promise<PluginVersion>


isAutoUpdateEnabled() => Promise<AutoUpdateEnabled>

获取自动更新配置的状态。

返回值: Promise<AutoUpdateEnabled>


removeAllListeners() => Promise<void>

移除该插件的所有监听器。

自: 1.0.0


addListener(eventName: 'download', listenerFunc: (state: DownloadEvent) => void) => Promise<PluginListenerHandle>

监听 App 中的 bundle 下载事件。下载开始、下载过程中和下载完成时都会触发。 下载期间会返回所有下载百分比。

参数类型
eventName‘download’
listenerFunc(state: DownloadEvent) => void

返回值: Promise<PluginListenerHandle>

自: 2.0.11


addListener(eventName: 'noNeedUpdate', listenerFunc: (state: NoNeedEvent) => void) => Promise<PluginListenerHandle>

监听无需更新事件,适合需要在每次启动 App 时强制检查更新的场景

参数类型
eventName‘noNeedUpdate’
listenerFunc(state: NoNeedEvent) => void

返回值: Promise<PluginListenerHandle>

自: 4.0.0


addListener(eventName: 'updateAvailable', listenerFunc: (state: UpdateAvailableEvent) => void) => Promise<PluginListenerHandle>

监听可用更新事件,适用于希望在每次启动应用时强制检查更新的场景

参数类型
eventName‘updateAvailable’
listenerFunc(state: UpdateAvailableEvent) => void

返回值: Promise<PluginListenerHandle>

自: 4.0.0


addListener(eventName: 'downloadComplete', listenerFunc: (state: DownloadCompleteEvent) => void) => Promise<PluginListenerHandle>

监听 downloadComplete 事件。

参数类型
eventName‘downloadComplete’
listenerFunc(state: DownloadCompleteEvent) => void

返回值: Promise<PluginListenerHandle>

自: 4.0.0


addListener(eventName: 'majorAvailable', listenerFunc: (state: MajorAvailableEvent) => void) => Promise<PluginListenerHandle>

监听 App 中的 Major 更新事件,当 Major 更新因设置 disableAutoUpdateBreaking 而被阻止时通知你。

参数类型
eventName‘majorAvailable’
listenerFunc(state: MajorAvailableEvent) => void

返回值: Promise<PluginListenerHandle>

自: 2.3.0


addListener('updateFailed', …)

“addListener(‘updateFailed’, …)”章节
addListener(eventName: 'updateFailed', listenerFunc: (state: UpdateFailedEvent) => void) => Promise<PluginListenerHandle>

监听 App 中的更新失败事件,在下次启动 App 时若更新安装失败会通知你

参数类型
eventName‘updateFailed’
listenerFunc(state: UpdateFailedEvent) => void

返回值: Promise<PluginListenerHandle>

自: 2.3.0


addListener('downloadFailed', …)

“addListener(‘downloadFailed’, …)”章节
addListener(eventName: 'downloadFailed', listenerFunc: (state: DownloadFailedEvent) => void) => Promise<PluginListenerHandle>

监听 App 中的下载失败事件,bundle 下载失败时会通知你

参数类型
eventName‘downloadFailed’
listenerFunc(state: DownloadFailedEvent) => void

返回值: Promise<PluginListenerHandle>

自: 4.0.0


addListener(eventName: 'appReloaded', listenerFunc: () => void) => Promise<PluginListenerHandle>

监听 App 中的重载事件,在重载发生后通知你

参数类型
eventName‘appReloaded’
listenerFunc() => void

返回值: Promise<PluginListenerHandle>

自: 4.3.0


addListener(eventName: 'appReady', listenerFunc: (state: AppReadyEvent) => void) => Promise<PluginListenerHandle>

在 App 中监听应用就绪事件,以便在应用准备就绪时通知您

参数类型
eventName‘appReady’
listenerFunc(state: AppReadyEvent) => void

返回值: Promise<PluginListenerHandle>

自: 5.1.0


isAutoUpdateAvailable() => Promise<AutoUpdateAvailable>

获取自动更新是否可用(未被 serverUrl 禁用)。

返回值: Promise<AutoUpdateAvailable>


getNextBundle() => Promise<BundleInfo | null>

获取应用重新加载时将使用的下一个 bundle。 如果未设置下一个 bundle,则返回 null。

返回值: Promise<BundleInfo | null>

自: 6.8.0


setShakeMenu(options: SetShakeMenuOptions) => Promise<void>

启用或禁用用于调试/测试的摇一摇菜单

参数类型描述
optionsSetShakeMenuOptions包含 enabled 布尔值,用于启用或禁用摇一摇菜单

自: 7.5.0


isShakeMenuEnabled() => Promise<ShakeMenuEnabled>

获取摇一摇菜单的当前状态

返回值: Promise<ShakeMenuEnabled>

自: 7.5.0


属性类型
bundleBundleInfo
属性类型
idstring
versionstring
downloadedstring
checksumstring
statusBundleStatus
属性类型
urlstring
属性类型
urlstring
属性类型
urlstring

此 URL 和版本用于从服务器下载 bundle。如果您使用 backend,所有信息将由 getLatest 方法提供。 如果您不使用 backend,则需要提供 bundle 的 URL 和版本。如果您使用 CLI 命令 encrypt 加密了 bundle,则必须提供 SessionKey,您应将其作为该命令的结果接收。

属性类型描述默认自
urlstring要下载的 bundle zip 文件(例如:dist.zip)的 URL。(可以是任意 URL,例如:Amazon S3、GitHub 标签,或你托管 bundle 的任何其他位置。)
versionstring此 bundle/版本的版本号 code/名称
sessionKeystring当 bundle 使用 session key 加密时,用于该更新的 session keyundefined4.0.0
checksumstring该更新的校验和,应为 sha256 格式;如果 bundle 已加密,还需使用私钥加密undefined4.0.0
manifestManifestEntry[]用于 Delta(manifest)多文件下载的 manifestundefined6.1.0
属性类型
file_namestring | null
file_hashstring | null
download_urlstring | null
属性类型
idstring
属性类型
bundlesBundleInfo[]
属性类型描述默认自
rawboolean是否返回原始 bundle 列表或 manifest。如果为 true,列表将尝试读取内部数据库,而不是磁盘上的文件。false6.14.0
属性类型
toLastSuccessfulboolean
属性类型
bundleBundleInfo
nativestring
属性类型
delayConditionsDelayCondition[]
属性类型描述
kindDelayUntilNext在 setMultiDelay 中设置延迟条件
valuestring
属性类型描述自
versionstringgetLatest 方法的返回值4.0.0
checksumstring6
majorboolean
messagestring
sessionKeystring
errorstring
oldstring
urlstring
manifestManifestEntry[]6.1
属性类型描述默认自
channelstring获取最新版本所用的 Channel。该 Channel 必须允许 ‘self_assign’ 才能生效undefined6.8.0
属性类型描述自
statusstring已设置 Channel 的当前状态4.7.0
errorstring
messagestring
属性类型
channelstring
triggerAutoUpdateboolean
属性类型
triggerAutoUpdateboolean
属性类型描述自
channelstring获取 channel 的当前状态4.8.0
errorstring
messagestring
statusstring
allowSetboolean
属性类型描述自
channelsChannelInfo[]可用频道列表7.5.0
属性类型描述自
idnumber数字通道 ID7.5.0
namestring通道名称7.5.0
publicboolean如果为 true,则这是默认/回退通道。设备无法自行分配到公共通道。相反,当设备移除其通道覆盖设置(使用 unsetChannel())时,它将自动接收来自匹配的公共通道的更新。7.5.0
allow_self_setboolean如果为 true,设备可以使用以下方式显式地自行分配到此通道: setChannel()。这通常用于 beta 测试、A/B 测试或选择性加入的更新轨道。7.5.0

通道命令使用通道 name而非此数字 id.

属性类型
customIdstring
属性类型
versionstring
属性类型
deviceIdstring
属性类型
versionstring
属性类型
enabledboolean
属性类型
remove() => Promise<void>
属性类型描述自
percentnumber下载当前状态,介于 0 到 100 之间。4.0.0
bundleBundleInfo
属性类型描述自
bundleBundleInfo下载当前状态,介于 0 到 100 之间。4.0.0
属性类型描述自
bundleBundleInfo当前下载状态,介于 0 到 100 之间。4.0.0
属性类型描述自
bundleBundleInfo当有新更新可用时触发。4.0.0
属性类型描述自
versionstring当有新的 major bundle 可用时触发。4.0.0
属性类型描述自
bundleBundleInfo更新安装失败时触发。4.0.0
属性类型描述自
versionstring下载失败时触发。4.0.0
属性类型描述自
bundleBundleInfo当应用准备就绪时触发。5.2.0
statusstring
属性类型
availableboolean
属性类型
enabledboolean
属性类型
enabledboolean

pending: bundle 等待被 SET 作为下一个 bundle。 downloading: bundle 正在下载。 success: bundle 已下载完成,准备好 SET 作为下一个 bundle。 错误:bundle 下载失败。

‘success’ | ‘error’ | ‘pending’ | ‘downloading’

‘background’ | ‘kill’ | ‘nativeVersion’ | ‘date’

如果你正在使用 函数与设置 要规划 dashboard 和 API 操作,请将其连接到 使用 @capgo/capacitor-updater 用于“使用 @capgo/capacitor-updater”中的原生能力, API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节,以及 设备 有关 Devices 的实现细节。