SDK with single-load API.
Rewarded ads let you reward users with in-app items for interacting with video ads, playable ads, and surveys.Prerequisites
Always test with test ads
When building and testing your apps, make sure that you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.
The easiest way to load test ads is to use the dedicated test ad unit ID for Android rewarded ads:
ca-app-pub-3940256099942544/5224354917
This ad unit ID has been specially configured to return test ads for every request. You're free to use it in your own apps while coding, testing, and debugging. Make sure you replace it with your own ad unit ID before publishing your app.
For details on GMA Next-Gen SDK test ads, see Enable test ads.
Load an ad
To optimize ad serving latency for multiple ads, consider using ad preloading instead.
The following example shows you how to load a single ad:
Kotlin
// Load ads after you initialize MobileAds. RewardedAd.load( AdRequest.Builder(adUnitId).build(), object : AdLoadCallback<RewardedAd> { override fun onAdLoaded(ad: RewardedAd) { // Rewarded ad loaded. rewardedAd = ad } override fun onAdFailedToLoad(adError: LoadAdError) { // Rewarded ad failed to load. Log.e(TAG, "Rewarded ad failed to load: ${adError.message}") rewardedAd = null } }, )
Java
// Load ads after you initialize MobileAds. RewardedAd.load( new AdRequest.Builder(adUnitId).build(), new AdLoadCallback<RewardedAd>() { @Override public void onAdLoaded(@NonNull RewardedAd ad) { // Rewarded ad loaded. rewardedAd = ad; } @Override public void onAdFailedToLoad(@NonNull LoadAdError adError) { // Rewarded ad failed to load. Log.e(TAG, "Rewarded ad failed to load: " + adError.getMessage()); rewardedAd = null; } });
When testing, use the test ad unit ID:
ca-app-pub-3940256099942544/5224354917.
Show the ad
To show a rewarded ad, use the show method. Use an
OnUserEarnedRewardListener object to handle reward events.
Kotlin
private fun showAd(rewardedAd: RewardedAd, activity: Activity) { // Show the ad. rewardedAd.show( activity, object : OnUserEarnedRewardListener { override fun onUserEarnedReward(rewardItem: RewardItem) { // User earned the reward. val rewardAmount = rewardItem.amount val rewardType = rewardItem.type } }, ) }
Java
private void showAd(RewardedAd rewardedAd, Activity activity) { // Show the ad. rewardedAd.show( activity, new OnUserEarnedRewardListener() { @Override public void onUserEarnedReward(@NonNull RewardItem rewardItem) { // User earned the reward. int rewardAmount = rewardItem.getAmount(); String rewardType = rewardItem.getType(); } }); }
Listen to ad events
Before showing the ad, listen to ad lifecycle events:
Kotlin
private fun listenToAdEvents() { // Listen for ad events. val ad = rewardedAd if (ad == null) { Log.e(TAG, "Rewarded ad is not ready yet.") return } ad.adEventCallback = object : RewardedAdEventCallback { override fun onAdShowedFullScreenContent() { // Rewarded ad did show. } override fun onAdDismissedFullScreenContent() { // Rewarded ad did dismiss. rewardedAd = null } override fun onAdFailedToShowFullScreenContent( fullScreenContentError: FullScreenContentError ) { // Rewarded ad failed to show. Log.e(TAG, "Rewarded ad failed to show: ${fullScreenContentError.message}") } override fun onAdImpression() { // Rewarded ad did record an impression. } override fun onAdClicked() { // Rewarded ad did record a click. } } }
Java
private void listenToAdEvents() { // Listen for ad events. if (rewardedAd == null) { Log.e(TAG, "Rewarded ad is not ready yet."); return; } rewardedAd.setAdEventCallback( new RewardedAdEventCallback() { @Override public void onAdShowedFullScreenContent() { // Rewarded ad did show. } @Override public void onAdDismissedFullScreenContent() { // Rewarded ad did dismiss. rewardedAd = null; } @Override public void onAdFailedToShowFullScreenContent( FullScreenContentError fullScreenContentError) { // Rewarded ad failed to show. Log.e(TAG, "Rewarded ad failed to show: " + fullScreenContentError.getMessage()); } @Override public void onAdImpression() { // Rewarded ad did record an impression. } @Override public void onAdClicked() { // Rewarded ad did record a click. } }); }
Optional: Validate server-side verification (SSV) callbacks
If your app requires extra data in server-side
verification
callbacks, 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 shows how to set custom data on a rewarded ad object before requesting an ad:
Kotlin
Java
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.