Rewarded video integration for Flutter

Before you start

Make sure you have correctly integrated the ironSource Flutter Plugin as well as any additional Ad Network Adapters into your application. The integration is outlined here.

Step 1. Implement the Rewarded Video Listener

The ironSource Flutter Plugin fires several events to inform you of ad availability and completions so you’ll know when to reward your users.

Set the rewarded video listener:

IronSource.setLevelPlayRewardedVideoBaseListener(mLevelPlayRewardedVideoListener);

The Plugin will notify the Listener of all possible events listed below:

abstract class LevelPlayRewardedVideoBaseListener {
 /// The Rewarded Video ad view has opened.
 ///
 /// Native SDK Reference
 /// - Android: onAdOpened
 /// -     iOS: didOpenWithAdInfo
 void onAdOpened(IronSourceAdInfo adInfo);
 /// The Rewarded Video ad view is about to be closed.
 ///
 /// Native SDK Reference
 /// - Android: onAdClosed
 /// -     iOS: didCloseWithAdInfo
 void onAdClosed(IronSourceAdInfo adInfo);
 /// The user completed to watch the video, and should be rewarded.
 /// - [placement] will include the reward data.
 /// - When using server-to-server callbacks,
 /// you may ignore this event and wait for the ironSource server callback.
 ///
 /// Native SDK Reference
 /// - Android: onAdRewarded
 /// -     iOS: didReceiveRewardForPlacement
 void onAdRewarded(
     IronSourceRewardedVideoPlacement placement, IronSourceAdInfo adInfo);
 /// The rewarded video ad failed to show.
 ///
 /// Native SDK Reference
 /// - Android: onAdShowFailed
 /// -     iOS: didFailToShowWithError
 void onAdShowFailed(IronSourceError error, IronSourceAdInfo adInfo);
 /// Invoked when the video ad was clicked.
 /// - This callback is not supported by all networks, and we recommend using it
 /// only if it's supported by all networks you included in your build.
 ///
 /// Native SDK Reference
 /// - Android: onAdClicked
 /// -     iOS: didClick
 void onAdClicked(
     IronSourceRewardedVideoPlacement placement, IronSourceAdInfo adInfo);
}

Error Codes
ironSource provides an error code mechanism to help you understand any error you may encounter during integration or in live production. See the complete guide here.

Network Connectivity Status
You can determine and monitor the internet connection on the user’s device through the ironSource Network Change Status function.  This enables the SDK to change its ad availability according to network modifications, i.e., in the case of no network connection, the availability will turn into “false”. The default of this function is “false”; if you’d like to listen to changes in the connectivity, activate it in the SDK initialization with the following API:

IronSource.shouldTrackNetworkState(true);

Step 2. Show a Video Ad to Your Users

Ad Availability

You will receive the availability status of a Rewarded Video ad through the onAdAvailable callback.

  void onAdAvailable(IronSourceAdInfo adInfo);
  /// Indicates that no ads are available to be displayed.
  /// - Use this callback instead of [onRewardedVideoAvailabilityChanged]
  ///
  /// Native SDK Reference
  /// - Android: onAdUnavailable
  /// -     iOS: hasNoAvailableAd
  void onAdUnavailable(); 
}

In addition, you have the possibility to ask for ad availability directly by calling:

bool isAvailable = await IronSource.isRewardedVideoAvailable();
Note: ironSource SDK automatically caches Rewarded Videos for you, to ensure you have ad availability throughout the entire session.

Serve a Video Ad

Once an ad network has a video available, you are ready to show the video to your users. Before you display the ad, make sure to pause any game actions including audio to ensure the best experience for your users. Call showRewardedVideo to show the loaded video ad to your users:

IronSource.showRewardedVideo();


Ad Placements

With the ironSource Ad Placements tool, you can customize and optimize the Rewarded Video experience. This tool enables you to present videos to your users from different placements depending on the reward. You can use the function below to define the exact Placement you’d like to show an ad from. Navigate to the Ad Placement document for more details.

IronSource.showRewardedVideo(placementName: 'YOUR_PLACEMENT_NAME');

To get the details of a specific reward associated with each ad placement, you can call the following:

IronSourceRewardedVideoPlacement placement = await IronSource.getRewardedVideoPlacementInfo(placementName: 'YOUR_PLACEMENT_NAME');
/// Placement would fallback to the default placement if [placementName] is invalid.
String rewardName = placement.rewardName;
int rewardAmount = placement.rewardAmount();

Note: Make sure you use the placement name as written in the platform when using it as a parameter.

Capping & Pacing

In addition to ironSource’s Ad Placements, you can now configure capping and pacing settings for selected placements. Capping and pacing improve the user experience in your app by limiting the number of ads served within a defined timeframe. Read more about capping and pacing here.

If you choose to use the capping and pacing tool for Rewarded Video, we recommend calling the function below to verify if a specific placement has reached its limit. This is to ensure not to display a Rewarded Video button when the placement has been capped or paced, and thus the video ad will not be served.

Future<bool> isRewardedVideoPlacementCapped({required String placementName});

Dynamic User ID

The Dynamic UserID is a parameter to verify AdRewarded transactions and can be changed throughout a session. To receive this parameter through server-to-server callbacks, it must be set before calling showRewardedVideo. You will receive a dynamicUserId parameter in a callback URL with reward details.

Future<bool> setDynamicUserId(String dynamicUserID);


Make sure you’ve set the userID before initializing the ironSource SDK in order to successfully reward your users.

Step 3. Reward the User

The ironSource SDK will fire the onAdRewarded event each time the user successfully completes a video. The LevelPlayRewardedVideoListener will be in place to receive this event so you can reward the user successfully.

 The onAdRewarded and onAdClosed events are asynchronous. Make sure to set up your listener to grant rewards even in cases where onAdRewarded is fired after the onAdClosed event.

@override
 void onAdRewarded(
     IronSourceRewardedVideoPlacement placement, IronSourceAdInfo adInfo){
  /// TODO: Here you can reward the user according to the reward name and amount
  String rewardName = placement.rewardName;
  int rewardAmount = placement.rewardAmount;
}

Note: The default setting in your ironSource account is to notify you of user completions/rewards via the onAdRewarded  callback within the client of your app. Additionally,  you can turn on server-to-server callbacks if you would also like to receive notifications to your back-end server.

Server-to-server callbacks

If you turn on server-to-server callbacks, make sure you’ve set the userID before you initialize the ironSource SDK so your users can get rewarded, and make sure to reward your user only once for the same completion.
ironSource LevelPlay will be firing both the client-side callback and the server-to-server callback. So, you will get two notifications in total for each completion.

To utilize server-to-server callbacks, see here.

Done!

You are now all set to deliver Rewarded Video in your application.