在 Kotlin Multiplatform SDK 中控制应用商店消息

App Store 和 Google Play 有时需要向用户显示有关其订阅的消息:

  • App Store:请求用户同意涨价、账单问题通知或赢回优惠。默认情况下,StoreKit 会在应用打开时自动显示这些消息。
  • Google Play:通知用户订阅付款被拒,需要更新付款方式。从 SDK 4.3.0-beta.1 开始,Adapty SDK 默认在每次 activate 之后、应用的 Activity 启动时(包括应用返回前台时)请求这些消息。

无论哪种方式,消息都可以显示在流程、用户引导或结账页面的上方。如需自行控制显示时机,请将 Adapty SDK 切换到手动消息处理模式,并在不影响用户操作时调用 showStoreMessages。

Warning

在手动模式下,应用商店不会自动显示消息。如果你的应用从未调用 showStoreMessages,用户将永远看不到价格上涨同意请求、账单问题通知或付款拒绝通知。

开始之前

你需要:

  • Adapty Kotlin Multiplatform SDK 4.3.0-beta.1 或更高版本:更早的版本不支持 store messages API。
  • iOS 16 或更高版本(适用于 App Store 消息):StoreKit 不会向更早版本的 iOS 应用推送消息。在 iOS 15 上,getPendingStoreMessageTypes 和 showStoreMessages 会抛出 AdaptyErrorCode.DECODING_FAILED 错误。如果你的应用需要支持 iOS 15,请在 onError 中处理该错误,如下方示例所示。

启用手动处理

Adapty SDK 通过 withStoreMessagesHandling 方法设置以下两种商店消息处理模式之一:

  • AdaptyStoreMessagesHandling.AUTO:默认模式。StoreKit 自动显示 App Store 消息,SDK 则在每次启动应用的 Activity 时请求 Google Play 消息。
  • AdaptyStoreMessagesHandling.MANUAL:仅当应用调用 showStoreMessages 时,商店才会显示消息。

要切换到手动模式,请在激活 SDK 时将 withStoreMessagesHandling 设置为 AdaptyStoreMessagesHandling.MANUAL:


val config = AdaptyConfig
    .Builder("PUBLIC_SDK_KEY")
    .withStoreMessagesHandling(AdaptyStoreMessagesHandling.MANUAL) // AUTO is the default
    .build()

Adapty.activate(configuration = config)

在 iOS 上,SDK 会将每条 App Store 消息添加到当前应用会话的待处理列表中。在 Android 上,SDK 会停止自行请求 Google Play 消息。使用默认的 AUTO 值时,SDK 不会捕获 App Store 消息,因此在 iOS 上,getPendingStoreMessageTypes 和 showStoreMessages 将没有任何内容可返回或显示。

获取待处理消息类型

在 iOS 上,若要查询是否有 App Store 消息待处理,请调用 getPendingStoreMessageTypes。该方法会返回待处理消息的类型,但不会展示或移除这些消息:


Adapty.getPendingStoreMessageTypes()
    .onSuccess { pendingTypes ->
        if (pendingTypes == null) {
            // Android: Google Play decides whether a message applies when you show it
            Adapty.showStoreMessages()
        } else if (AdaptyStoreMessageType.BILLING_ISSUE in pendingTypes) {
            // For example, skip the upsell and show the billing message first
        }
    }
    .onError { error ->
        // On iOS 15, the call fails with AdaptyErrorCode.DECODING_FAILED (2006)
    }

在 Android 上,该方法不会调用原生 SDK,直接返回 null:Google Play 不会主动上报待处理消息,而是在应用请求时才判断某条消息是否适用。空列表表示 iOS 上没有待处理消息。在 activate 完成之前,列表始终为空。

每条 App Store 消息都属于以下类型之一:

类型消息
AdaptyStoreMessageType.GENERIC通用 App Store 消息。
AdaptyStoreMessageType.PRICE_INCREASE_CONSENT订阅价格上涨的用户同意请求。
AdaptyStoreMessageType.BILLING_ISSUE订阅账单问题通知。适用于 iOS 16.4 及更高版本。
AdaptyStoreMessageType.WIN_BACK_OFFER符合条件的赢回优惠。适用于 iOS 18 及更高版本。

新的 StoreKit 消息类型以 AdaptyStoreMessageType 的形式传入,其 value 为 storekit_<number>。对于表格中未列出的任何值,应将其视为 SDK 无法识别的类型。

显示消息

当应用到达一个合适的时机——例如用户关闭流程或完成用户引导之后——调用 showStoreMessages,此时显示商店消息不会打断用户:


Adapty.showStoreMessages()
    .onError { error ->
        // handle the error
    }
  • iOS:SDK 会逐条显示待处理消息。StoreKit 显示某条消息后,SDK 会将其从待处理列表中移除。若 StoreKit 无法显示某条消息,SDK 会保留该消息留待下次调用,并继续处理其他消息。
  • Android:Adapty SDK 会请求 Google Play 在应用当前的 Activity(即显示应用界面的 Android 组件)上展示消息。SDK 通过 Android 的生命周期回调获知当前 Activity 是哪个。Google Play 会自行判断消息是否适用于该用户,因此请求并不一定会显示消息。调用会立即返回,不会等待 Google Play,也不会上报 Google Play 的错误。如果用户通过消息修复了支付方式,SDK 会将该用户的购买记录同步到 Adapty,并通过用户画像监听器接收更新后的访问等级。

在 Android 上启动时显示消息

在默认的 AUTO 模式下,SDK 不会为在 Adapty.activate 调用之前启动的 Activity 请求消息,因此 Google Play 消息不会在启动时显示。若要在启动时显示消息,请仅在 Android 上,在 activate 之后调用一次 showStoreMessages:

// commonMain
expect val isAndroid: Boolean

// androidMain
actual val isAndroid: Boolean = true

// iosMain
actual val isAndroid: Boolean = false

// commonMain — activate the SDK

Adapty.activate(configuration = AdaptyConfig.Builder("PUBLIC_SDK_KEY").build())
    .onSuccess {
        if (isAndroid) {
            Adapty.showStoreMessages()
        }
    }

请勿在 iOS 上调用此方法:在 AUTO 模式下,该方法不会显示任何内容,且在 iOS 15 上会报错。

仅显示特定类型的消息

如需仅显示部分待处理的 App Store 消息,请在 iosFilter 中传入对应类型,其余消息将保持待处理状态:


Adapty.showStoreMessages(
    iosFilter = listOf(
        AdaptyStoreMessageType.BILLING_ISSUE,
        AdaptyStoreMessageType.PRICE_INCREASE_CONSENT
    )
)

如果不设置 iosFilter,SDK 会显示所有待处理的消息,包括它无法识别的类型。传入空列表则不显示任何消息。Android 会忽略 iosFilter,并请求所有 Google Play 消息。

错误

在 iOS 上,showStoreMessages 会返回一个 AdaptyError,其错误码如下:

代码名称原因
2006AdaptyErrorCode.DECODING_FAILED设备运行的是 iOS 15 或更早版本。请仅在 iOS 16 及更高版本上调用此方法。
3201AdaptyErrorCode.OPERATION_IN_PROGRESS另一个 showStoreMessages 调用仍在显示消息,而本次调用也有消息需要显示。请等待第一个调用完成后再重试。
3202AdaptyErrorCode.RESOLVER_FAILURESDK 未找到处于前台活跃状态的窗口场景来显示消息。在报错前已显示的消息将从待处理列表中移除。请在应用处于前台时再次调用该方法,以显示剩余消息。