Skip to content
This repository has been archived by the owner on Mar 12, 2022. It is now read-only.

Creating a rewarded interstitial ad

Bruno D'Luka edited this page Mar 15, 2021 · 2 revisions

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.

Creating an ad object

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.

Load the ad

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));

Show the ad

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();
  },
),

Listening to events

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;
  }
});

Using the listener to reload

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;
  }
});

Do NOT show a new ad in RewardedAdEvent.closed, because it'll become infinite.