Reminder: By Aug 31, 2025, all new apps and updates to existing apps must use Billing Library version 7 or newer. If you need more time to update your app, you can request an extension until Nov 1, 2025. Learn about Play Billing Library version deprecation.
Google Play 提供实时开发者通知 (RTDN),可监控购买生命周期事件。如需根据这些事件执行必要的操作,请使用适用于订阅和应用内购买的 Play Developer API。只要使用这些工具并构建完善的购买生命周期管理系统,您就可以提供无缝的用户体验,并高效地管理购买交易和权限。
构建实时开发者通知客户端
在 Google Play 结算系统上进行的购买交易可能会在其生命周期中发生多次权限更改。许多操作都可能触发这些更改,包括:
用户在应用中发起的操作。
用户通过 Play 商店应用发起的操作。
您直接从后端系统发起的操作。
您通过 Google Play 管理中心发起的操作。
例如:
用户通过 Play 商店订阅中心取消订阅。
开发者使用 Google Play Developer API 推迟订阅结算。
开发者通过 Google Play 管理中心为购买交易发放退款并撤消权限。
后端必须了解购买交易可能会经历的不同状态,并据此采取所有必要的措施来及时调整权限。
虽然可以使用 Google Play Developer API 手动检查购买交易状态,但通过定期检查来跟踪更改,不仅效率不高,并且容易出错和发生延迟。RTDN 有助于您立即响应更改,且无需为 Google Play 购买交易构建生命周期跟踪逻辑。
本部分介绍如何为 RTDN 构建客户端。RTDN 是使用 Google Cloud Pub/Sub 构建的一个功能,可在用户权限状态发生变化时,向后端发送即时通知。Pub/Sub 系统包括发送通知的发布端和订阅通知的客户端。通过实现 RTDN,您可以实时跟踪并及时响应用户权限状态的所有变化。
RTDN 发布端
Google Play 的后端可充当 RTDN 的发布端。如需为您的应用设置 RTDN,请按照设置指南中的说明操作。完成这些步骤后,Google Play 结算系统就能充当您应用的 RTDN 发布端。如需完成此设置,您应熟悉 Google Cloud Platform Console,以设置基本的 Pub/Sub 配置。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Purchase lifecycle and RTDNs\n\nWhen you sell digital products through your app, you must consider the entire\nuser experience. In-app integration lets you launch purchase flows and\nmanage the user experience, but it's crucial to keep your backend up-to-date on\nthe entitlements that users are purchasing. This is important for tracking\npurchases and managing other aspects of the user experience, such as\ncross-platform entitlements.\n\nTo monitor purchase lifecycle events and respond quickly to changes in user\nentitlements, you should build a purchase status management system in your\nbackend for both subscriptions and one-time purchases. This system ensures quick\nand secure purchase processing regardless of device status, maintains consistent\nuser entitlements across all platforms, and provides the ability to consult\npurchase history and entitlement data in your backend.\n\nGoogle Play offers [real-time developer notifications\n(RTDN)](/google/play/billing/rtdn-reference) to monitor purchase lifecycle\nevents, and the Play Developer APIs for [Subscriptions and In-App\nPurchases](https://developers.google.com/android-publisher#subscriptions) can be\nused to take necessary actions based on these events. By using these tools and\nbuilding a robust purchase lifecycle management system, you can provide a\nseamless user experience and manage purchases and entitlements efficiently.\n\nBuild a real-time developer notification client\n-----------------------------------------------\n\nPurchases made on Google Play's billing system can go through several entitlement\nchanges throughout their lifecycle. Various actions can trigger these changes,\nincluding the following:\n\n- Actions initiated by users in your app.\n- Actions initiated by users through the Play Store app.\n- Actions initiated directly from your backend systems.\n- Actions that you initiate through the Google Play Console.\n\nFor example:\n\n- A user canceling a subscription through the Play Store subscription center.\n- A developer deferring subscription billing using the Google Play Developer API.\n- A developer issuing a refund and revoking entitlement for a purchase through the Google Play Console.\n\nIt is crucial that your backend is aware of the different states a purchase can\ngo through and that it takes all necessary measures to adjust the entitlement\naccordingly in a timely manner.\n\nWhile it is possible to use the Google Play Developer API to check a purchase\nstatus manually, relying on periodic checks is a very inefficient way to track\nchanges and it's prone to errors and delays. RTDNs can help you respond to\nchanges immediately without having to build lifecycle tracking logic for your\nGoogle Play purchases.\n\nThis section discusses how to build a client for RTDNs. RTDN is a feature built\nusing Google Cloud Pub/Sub, which sends your backend an instant notification\nwhen a user's entitlement state changes. The Pub/Sub system consists of a\npublisher that sends notifications and a client that subscribes to those\nnotifications. By implementing RTDN, you can track all changes to the user's\nentitlement state in real time and respond to them promptly.\n\n### RTDN publisher\n\nGoogle Play's backend acts as the publisher for RTDNs. To set up RTDN for you\napp, follow the instructions in the [Setup](/google/play/billing/getting-ready)\nguide. These steps allow Google Play's billing system to act as the publisher for\nyour app's RTDNs. To complete this setup, you should familiarize yourself with\nthe Google Cloud Platform Console to set up a basic Pub/Sub configuration.\n\n### RTDN subscriber\n\nAfter setting up the publisher, you should prepare your backend to consume\nRTDNs. To do this, you need to build a client to receive Google Cloud Pub/Sub\nmessages. Your RTDN client's basic function consists of receiving instances of\n[`PubSubMessage`](https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage),\neither through HTTPS requests in a registered endpoint or by using the [Cloud\nPub/Sub client\nlibraries](https://cloud.google.com/pubsub/docs/reference/libraries). See the\nPub/Sub documentation to learn about using a\n[push](https://cloud.google.com/pubsub/docs/push) or a\n[pull](https://cloud.google.com/pubsub/docs/pull) strategy, or the [RTDN setup\ndocumentation](/google/play/billing/getting-ready#create-sub) for guidelines on\nchoosing the strategy that works best for your needs.\n\nFor each message you receive, your backend should do the following:\n\n- Unpack the base-64-encoded `data` field, which contains the [RTDN\n object](/google/play/billing/rtdn-reference#encoding).\n- Trigger any required backend processes related to the entitlement change notified by the RTDN event.\n\nHandle purchase state transitions\n---------------------------------\n\nOne-time purchases and subscription purchases have different lifecycles based on\nthe different states and events that can affect them. Thanks to RTDN, you don't\nneed to build logic to confirm state transitions. All you need to do is define\nwhat happens when your backend receives each type of notification.\n| **Note:** For [pre-orders](/google/play/billing/multi-offer-one-time-product#pre-order), RTDN follows the same lifecycle as the one-time purchase.\n\nSee the following guides to learn more about these scenarios:\n\n- [One-time purchase lifecycle](/google/play/billing/lifecycle/one-time)\n- [Subscription purchase lifecycle](/google/play/billing/lifecycle/subscriptions)"]]