-
-
Notifications
You must be signed in to change notification settings - Fork 58
Creating a rewarded interstitial ad
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.
To create a rewarded interstitial ad, use the class RewardedInterstitialAd
:
// You can set the unit id on the constructor
RewardedInterstitialAd rewardedInterstitialAd = RewardedInterstitialAd(/* unitId: MobileAds.rewardedInterstitialAdVideoTestUnitId */);
A single InterstitialAd object can be used to request and display multiple interstitial ads, so you only need
to construct it once.
In order to show an ad, it needs to be loaded first. You can use load() to load the ad:
rewardedInterstitialAd.load();
You can load an ad right when you initalize the ad:
RewardedInterstitialAd rewardedInterstitialAd = RewardedInterstitialAd()..load(timeout: Duration(minutes: 1));
Interstitial ads should be displayed during natural pauses in the flow of an app. Between levels of a game is a good example, or after the user completes a task. To show an interstitial, use the isAvailable
method to verify that it's done loading, then call show()
. The interstitial ad from the previous code example could be shown in a button's onPressed
like this:
FlatButton(
child: Text('Open rewarded interstitial ad'),
onPressed: () async {
// Load only if not loaded
if (!rewardedInterstitialAd.isAvailable) await rewardedInterstitialAd.load();
if (rewardedInterstitialAd.isAvailable) rewardedInterstitialAd.show();
},
),
To further customize the behavior of your ad, you can hook onto a number of events in the ad's lifecycle: loading, opening, closing, and so on. You can listen for these events using rewardedInterstitialAd.onEvent.listen((_) {...}):
rewardedInterstitialAd.onEvent.listen((e) {
final event = e.keys.first;
switch (event) {
case RewardedAdEvent.loading:
print('loading');
break;
case RewardedAdEvent.loaded:
print('loaded');
break;
case RewardedAdEvent.loadFailed:
final errorCode = e.values.first;
print('load failed $errorCode');
break;
case RewardedAdEvent.opened:
print('ad opened');
break;
case RewardedAdEvent.closed:
print('ad closed');
break;
case RewardedAdEvent.earnedReward:
final reward = e.values.first;
print('earned reward: $reward');
break;
case RewardedAdEvent.showFailed:
final errorCode = e.values.first;
print('show failed $errorCode');
break;
default:
break;
}
});
RewardedAdEvent.closed
is a handy place to load a new interstitial after displaying the previous one:
rewardedInterstitialAd.onEvent.listen((e) {
final event = e.keys.first;
switch (event) {
case RewardedAdEvent.closed:
rewardedAd = RewardedAd.load();
break;
default:
break;
}
});