Rewarded interstitial is a type of incentivized ad format that allows you offer rewards for ads that appear automatically during natural app transitions. Unlike rewarded ads, users aren't required to opt-in to view a rewarded interstitial.
Prerequisites
- Google Mobile Ads SDK 19.2.0 or higher.
- Follow the Get Started guide to
import the Google Mobile Ads SDK and update your
AndroidManifest.xml
. - Reach out to your account manager to get access to rewarded interstitial ads.
Implementation
The main steps to integrate rewarded interstitial ads are:
- Load an ad.
- Register for full screen event callbacks.
- Handle the reward callback.
- Display the ad.
Load an ad
Loading an ad is accomplished using the static load()
method on the
RewardedInterstitialAd
class. The load method requires a Context, your ad
unit ID, an AdRequest
object, and a
RewardedInterstitialAdLoadCallback
to be notified when ad loading succeeds or
fails. The loaded RewardedInterstitialAd
object is provided as a parameter in
the onRewardedInterstitialAdLoaded()
callback. The following example shows how
to load a RewardedInterstitialAd
in your MainActivity
.
public class MainActivity extends AppCompatActivity { private RewardedInterstitialAd rewardedInterstitialAd; private String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { loadAd(); } }); public void loadAd() { // Use the test ad unit ID to load an ad. RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379", new AdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() { @Override public void onAdLoaded(RewardedInterstitialAd ad) { rewardedInterstitialAd = ad; Log.e(TAG, "onAdLoaded"); } @Override public void onAdFailedToLoad(LoadAdError loadAdError) { Log.e(TAG, "onAdFailedToLoad"); } }); } }
Register for callbacks
In order to receive notifications for presentation events, you must pass a
FullScreenContentCallback
object to the setter on your ad. The
FullScreenContentCallback
object handles callbacks for when the ad presents
successfully or unsuccessfully, and when it is dismissed. The code below shows
how to set an anonymous FullScreenContentCallback
object within your
RewardedInterstitialAdLoadCallback
:
public void loadAd(){ RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379", new AdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() { @Override public void onAdLoaded(RewardedInterstitialAd ad) { rewardedInterstitialAd = ad; rewardedInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() { /** Called when the ad failed to show full screen content. */ @Override public void onAdFailedToShowFullScreenContent(AdError adError) { Log.i(TAG, "onAdFailedToShowFullScreenContent"); } /** Called when ad showed the full screen content. */ @Override public void onAdShowedFullScreenContent() { Log.i(TAG, "onAdShowedFullScreenContent"); } /** Called when full screen content is dismissed. */ @Override public void onAdDismissedFullScreenContent() { Log.i(TAG, "onAdDismissedFullScreenContent"); } }); } @Override public void onAdFailedToLoad(LoadAdError loadAdError) { Log.e(TAG, "onAdFailedToLoad"); } }); }
Handle rewards
To display your rewarded interstitial ad, implement the
OnUserEarnedRewardListener
interface in your MainActivity
, to be notified when
the user earns a reward.
public class MainActivity extends AppCompatActivity implements OnUserEarnedRewardListener {
...
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.i(TAG, "onUserEarnedReward");
// TODO: Reward the user!
}
}
Show the ad
After implementing the OnUserEarnedRewardListener
interface, you can present
the ad using the ad's show()
method like so:
rewardedInterstitialAd.show(/* Activity */ MainActivity.this,/*
OnUserEarnedRewardListener */ MainActivity.this);