Skip to main content
Version: v1.1

Rewarded ads

Prerequisites

Load a rewarded ad object

Note: Make all calls to the Mobile Ads SDK on the main thread.

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 leverages LoadAdError to provide higher fidelity error details.

import com.google.android.gms.ads.rewarded.RewardedAd;

public class MainActivity extends Activity {
private RewardedAd premiumRewardedAd;
private final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
AdManagerAdRequest adRequest = new AdManagerAdRequest.Builder().build();
RewardedAd.load(this, "/361045431/example/rewarded",
adRequest, new RewardedAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d(TAG, loadAdError.toString());
premiumRewardedAd = null;
}

@Override
public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
premiumRewardedAd = rewardedAd;
Log.d(TAG, "Ad was loaded.");
}
});
}
}

[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.

RewardedAd.load(MainActivity.this, "/361045431/example/rewarded",
new AdRequest.Builder().build(), new RewardedAdLoadCallback() {
@Override
public void onAdLoaded(RewardedAd ad) {
Log.d(TAG, "Ad was loaded.");
premiumRewardedAd = ad;
ServerSideVerificationOptions options = new ServerSideVerificationOptions
.Builder()
.setCustomData("SAMPLE_CUSTOM_DATA_STRING")
.build();
premiumRewardedAd.setServerSideVerificationOptions(options);
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
premiumRewardedAd = null;
}
});

If you want to set the custom reward string, you must do so before showing the ad.

Key Point: The custom reward string is percent escaped and might require decoding when parsed from the SSV callback.

Set the FullScreenContentCallback

The FullScreenContentCallback handles events related to displaying your RewardedAd. Before you show your RewardedAd, make sure to set the callback like so:

premiumRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}

@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
premiumRewardedAd = null;
}

@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
premiumRewardedAd = null;
}

@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}

@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});

Show the ad

When you show a rewarded ad, you will use an OnUserEarnedRewardListener object to handle reward events.

if (premiumRewardedAd != null) {
Activity activityContext = MainActivity.this;
premiumRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// Handle the reward.
Log.d(TAG, "The user earned the reward.");
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
} else {
Log.d(TAG, "The rewarded ad wasn't ready yet.");
}