Rewarded ads
Prerequisites
- Complete the Get started guide.
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 leverages LoadAdError to provide higher fidelity error details.
- Java
- Kotlin
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.");
}
});
}
}
class MainActivity : AppCompatActivity() {
private var premiumRewardedAd: RewardedAd? = null
private final var TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var adRequest = AdManagerAdRequest.Builder().build()
RewardedAd.load(this,"/361045431/example/rewarded", adRequest, object : RewardedAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
mRewardedAd = null
}
override fun onAdLoaded(rewardedAd: RewardedAd) {
Log.d(TAG, "Ad was loaded.")
premiumRewardedAd = rewardedAd
}
})
}
}
[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
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;
}
});
RewardedAd.load(this, "/361045431/example/rewarded",
AdRequest.Builder().build(), object : RewardedAdLoadCallback() {
override fun onAdLoaded(ad: RewardedAd) {
Log.d(TAG, "Ad was loaded.")
premiumRewardedAd = ad
val options = ServerSideVerificationOptions.Builder()
.setCustomData("SAMPLE_CUSTOM_DATA_STRING")
.build()
premiumRewardedAd.setServerSideVerificationOptions(options)
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
premiumRewardedAd = null
}
})
If you want to set the custom reward string, you must do so before showing the ad.
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:
- Java
- Kotlin
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.");
}
});
premiumRewardedAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.")
}
override fun 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 fun onAdFailedToShowFullScreenContent(adError: AdError?) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.")
premiumRewardedAd = null
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}
override fun 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.
- Java
- Kotlin
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.");
}
if (premiumRewardedAd != null) {
premiumRewardedAd?.show(this, OnUserEarnedRewardListener() {
fun onUserEarnedReward(rewardItem: RewardItem) {
var rewardAmount = rewardItem.amount
var rewardType = rewardItem.type
Log.d(TAG, "User earned the reward.")
}
})
} else {
Log.d(TAG, "The rewarded ad wasn't ready yet.")
}