page.title=In-app Promotions parent.title=In-app Billing parent.link=index.html page.metaDescription=Support promo codes in your app, which lets you give content or features away to a limited number of users free of charge. page.image=/images/play_dev.jpg page.tags="promotions, billing, promo codes" meta.tags="monetization, inappbilling, promotions" @jd:body
Promo codes let you give content or features away to a limited number of users free of charge. After you create a promo code, you can distribute it subject to the terms of service. The user enters the promo code in your app or in the Google Play Store app and receives the item at no cost. You can use promo codes in many ways to creatively engage with users, such as the following:
Every promo code is associated with a particular product ID (also known as a SKU). You can create promo codes for your existing in-app products. You can also keep an SKU off the Play Store, so that the only way to get that item is by entering that SKU's promo code. When a user enters the promo code in the Play Store or in an app, the user gets the item, just as if they paid full price for it. If your app already uses In-app Billing Version 3 to support in-app purchases, it's easy to add support for promo codes.
You create promo codes through the Google Play Developer Console. Each promo code is associated with a single product registered in the developer console.
A user can redeem a promo code in one of these two ways:
To support promotion codes, your app must call the getPurchases()
method whenever the app starts or resumes. This method returns a bundle of all
current, unconsumed purchases, including purchases the user made by redeeming
a promo code. The simplest approach is to call getPurchases()
in your activity's {@link android.app.Activity#onResume onResume()} method,
since that callback fires when the activity is created, as well as when the
activity is unpaused. Calling getPurchases()
on startup and resume guarantees that your app finds out about all
purchases and redemptions the user may have made while the app wasn't
running. Furthermore, if a user makes a purchase while the app is running and
your app misses it for any reason, your app still finds out about the
purchase the next time the activity resumes and calls getPurchases()
.
Your app should allow users to redeem promo codes inside the app
itself. If your app supports the in-app purchase workflow (described in
Making
In-app Billing requests), your app automatically supports in-app
redemption of promo codes. When you launch the in-app purchase UI,
the user has the option to pay for the purchase with
a promo code. Your activity's {@link android.app.Activity#onActivityResult
onActivityResult()} method receives a response intent telling the app whether the
purchase was completed. However, your app should still call getPurchases()
on startup and resume, in case the purchase and consumption workflow
didn't complete. For example, if the user successfully redeems a promo code
and then your app crashes before the item is consumed, your app still receives
information about the purchase when the app calls getPurchases()
on its next startup.
Your app should also support the scenario where a user redeems a promo code
in the Play Store app while the app is running. Your app can find out right
away when the user redeems a code by registering a listener for the
PURCHASES_UPDATED
intent. The Play Store fires this intent
whenever a user redeems a promo code.
To listen for the PURCHASES_UPDATED
intent, dynamically create a
{@link android.content.BroadcastReceiver} object and register it to listen
for com.android.vending.billing.PURCHASES_UPDATED
. Register
the receiver by inserting code similar to the following in your activity's {@link
android.app.Activity#onResume onResume()} method:
IntentFilter promoFilter = new IntentFilter("com.android.vending.billing.PURCHASES_UPDATED"); registerReceiver(myPromoReceiver, promoFilter);
When the user makes a purchase, the system invokes your broadcast receiver's
{@link android.content.BroadcastReceiver#onReceive onReceive()} method. That
method must call getPurchases()
to see what purchases the user has made.
To reduce system overhead when your app isn't running, your activity's {@link android.app.Activity#onPause onPause()} method must unregister the broadcast receiver:
unRegisterReceiver(myPromoReceiver);
Note: Don't register this broadcast receiver in the
app manifest. Declaring the receiver in the manifest can cause the system to
launch the app to handle the intent if the user makes a purchase while the app
isn't running. This behavior is not necessary and may be annoying to the
user.
To find out about any purchases the user made while the app wasn't running,
call getPurchases()
when the user launches the app.
If your app supports in-app promotions, test the following use cases.
If the user redeems a promo code within the app's purchase flow, as described in Making In-app Billing requests, the system invokes your activity's {@link android.app.Activity#onActivityResult onActivityResult()} method to handle the purchase. Verify that {@link android.app.Activity#onActivityResult onActivityResult()} handles the purchase properly, whether the user pays with money or a promo code.
If the user redeems a promo code in the Play Store, there are several possible workflows. Verify each one of these workflows.
If the user redeems a promo code for an app that is not installed on the device, the Play Store prompts the user to install the app. (If the app is installed but not up-to-date, the Play Store prompts the user to update the app.) Test the following sequence on a device that doesn't have your app installed.
getPurchases()
and correctly detects the purchase the user made with the promo code.
If the user redeems a promo code for an app that is installed on the device, the Play Store prompts the user to switch to the app. Test the following sequence on a device that has your app installed but not running:
getPurchases()
and correctly detects the purchase the user made with the promo code.
If the user redeems a promo code for an app that is currently running on the
device, the Play Store notifies the app via a PURCHASES_UPDATED
intent. Test the following sequence:
PURCHASES_UPDATED
intent.
PURCHASES_UPDATED
intent. Verify that your app's
{@link android.content.BroadcastReceiver#onReceive
BroadcastReceiver.onReceive()} callback fires to handle the intent.
getPurchases()
. Verify that your app calls this method and that
it correctly detects the purchase the user made with the promo code.