Page Summary
-
Rewarded ads allow you to give users in-app items for interacting with ads.
-
Always use the provided test ad unit ID during development to avoid account suspension.
-
Load rewarded ads using the static
load()method of theRewardedAdclass. -
Set the
FullScreenContentCallbackto handle events related to the rewarded ad's display. -
Use an
OnUserEarnedRewardListenerobject when showing a rewarded ad to handle reward events.
Before you begin
Before you continue, do the following:
- Set up Google Mobile Ads SDK (Legacy).
- Use the test rewarded ad unit ID
/21775744923/example/rewarded.- When building and testing your apps, make sure that you use test ads rather than live, production ads. If you don't use the test ad unit ID, Google can suspend your account.
- Before you publish your app, replace this ID with your ad unit ID.
- For details on Google Mobile Ads SDK (Legacy) test ads, see Enable test ads.
Load a rewarded ad object
Rewarded ads are loaded by calling the static load method on the
RewardedAd class and passing in a RewardedAdLoadCallback. This is usually
done in the onCreate method of an Activity.
Notice that like other format load callbacks, RewardedAdLoadCallback
uses LoadAdError to provide higher fidelity error details.
Java
Kotlin
Replace AD_UNIT_ID with your ad unit ID.
Set the FullScreenContentCallback
The FullScreenContentCallback handles events related to displaying your
RewardedAd. Before you show your RewardedAd, make sure to set the callback
as follows:
Java
rewardedAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d(TAG, "Ad was dismissed.");
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
rewardedAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d(TAG, "Ad failed to show.");
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
rewardedAd = null;
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}
@Override
public void onAdClicked() {
// Called when an ad is clicked.
Log.d(TAG, "Ad was clicked.");
}
});
Kotlin
rewardedAd?.fullScreenContentCallback =
object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d(TAG, "Ad was dismissed.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
rewardedAd = null
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
// Called when fullscreen content failed to show.
Log.d(TAG, "Ad failed to show.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
rewardedAd = null
}
override fun onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(TAG, "Ad showed fullscreen content.")
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}
override fun onAdClicked() {
// Called when an ad is clicked.
Log.d(TAG, "Ad was clicked.")
}
}
Show the ad
When you show a rewarded ad, use an OnUserEarnedRewardListener object
to handle reward events.
Java
rewardedAd.show(
MainActivity.this,
new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.d(TAG, "User earned the reward.");
// Handle the reward.
}
});
Kotlin
rewardedAd?.show(
this,
OnUserEarnedRewardListener { rewardItem ->
Log.d(TAG, "User earned the reward.")
// Handle the reward.
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
},
)
For ads Google serves, Google Mobile Ads SDK (Legacy) calls the
onUserEarnedReward
method before the
onAdDismissedFullScreenContent
method. For ads served with mediation, the third-party ad source
determines the callback order. For ad sources that provide a single close
callback with reward information, the mediation adapter calls the
onUserEarnedReward method before the onAdDismissedFullScreenContent
method. For details, see
Set up mediation.
Optional: Validate server-side verification (SSV) callbacks
Apps that require extra data in server-side verification
callbacks should use the custom data feature of rewarded ads. Any string value
set on a rewarded ad object is passed to the custom_data query parameter of
the SSV callback. If no custom data value is set, the custom_data query
parameter value won't be present in the SSV callback.
The following code sample demonstrates how to set custom data on a rewarded ad object before requesting an ad.
Java
Kotlin
Replace SAMPLE_CUSTOM_DATA_STRING with your custom
data.
If you want to set the custom reward string, you must do so before showing the ad.
Examples on GitHub
Next steps
Explore the following topics: