Table of Contents

Class AdMobRewardedAdProvider

Namespace
VpnHood.AppLib.Ads.AdMob.Android
Assembly
VpnHood.AppLib.Ads.AdMob.Android.dll

Provides AdMob's rewarded ads.

public class AdMobRewardedAdProvider : IAdProvider, IDisposable
Inheritance
AdMobRewardedAdProvider
Implements
Inherited Members

Constructors

AdMobRewardedAdProvider(string)

Provides AdMob's rewarded ads.

public AdMobRewardedAdProvider(string adUnitId)

Parameters

adUnitId string

The AdMob ad unit's ID; AdMobTestAdUnitIds has Google's test units.

Properties

AdEarnedTimeout

Gets or sets how long, after the ad has closed, the provider waits for AdMob's SDK to report the reward earned. The default is 3 seconds.

public TimeSpan AdEarnedTimeout { get; set; }

Property Value

TimeSpan

Remarks

It counts whole milliseconds, a fraction dropped toward zero: −1 ms (InfiniteTimeSpan) waits without a limit, and a value outside −1 ms to 4,294,967,294 ms makes ShowAd(IUiContext, string?, CancellationToken) throw ArgumentOutOfRangeException once the ad has closed.

AdLifeSpan

Gets how long a loaded ad may wait before it is shown.

public TimeSpan AdLifeSpan { get; }

Property Value

TimeSpan

Remarks

It is 20 minutes.

AdLoadedTime

Gets when the ad loaded now was loaded, in UTC, or null where none is.

public DateTime? AdLoadedTime { get; }

Property Value

DateTime?

AdType

Gets the kind of ad the provider shows.

public AdType AdType { get; }

Property Value

AdType

NetworkName

Gets the ad network's name.

public string NetworkName { get; }

Property Value

string

Remarks

It is AdMob.

Methods

Create(string)

Creates a provider of AdMob's rewarded ads.

public static AdMobRewardedAdProvider Create(string adUnitId)

Parameters

adUnitId string

The AdMob ad unit's ID.

Returns

AdMobRewardedAdProvider

The provider.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Remarks

It does nothing: a loaded ad stays.

LoadAd(IUiContext, CancellationToken)

Loads an ad, to show it later.

public Task LoadAd(IUiContext uiContext, CancellationToken cancellationToken)

Parameters

uiContext IUiContext

The app's UI context.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that completes once an ad is loaded.

Remarks

It gathers the person's consent where one is due and starts AdMob's SDK, then loads the ad on the UI thread. A load that gets that far drops the ad loaded before; one that fails earlier, as for a destroyed activity, keeps it.

Exceptions

NoFillAdException

The network had no ad to fill the request.

LoadAdException

No ad could be loaded, as where the consent the network needs is missing.

AdException

The network's SDK could not start, as AdMob's with no ad adapter.

NotSupportedException

The network's SDK does not run on this Android version.

TimeoutException

The network's SDK did not start in time.

InvalidOperationException

The network's SDK did not start its initialization.

OperationCanceledException

cancellationToken was canceled.

LoadAdException

The activity is destroyed, the consent AdMob needs is missing, or AdMob could not load an ad.

ShowAd(IUiContext, string?, CancellationToken)

Shows the ad loaded.

public Task<ShowAdResult> ShowAd(IUiContext uiContext, string? customData, CancellationToken cancellationToken)

Parameters

uiContext IUiContext

The app's UI context.

customData string

The data the ad network passes to the server's check of a rewarded ad, or null.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<ShowAdResult>

A task whose result is how the ad ended.

Remarks

It shows the ad over the activity, with customData for AdMob's server-side verification of the reward, which it does not wait for, and completes once the person has closed it and AdMob's SDK has reported the reward earned, which it waits AdEarnedTimeout for. A show that starts drops the loaded ad however it ends; one that fails its checks keeps it. A cancel ends the wait, not an ad already shown.

Exceptions

ShowAdException

The ad could not be shown.

RewardNotEarnedException

For a rewarded ad, the network did not confirm the reward in time after the ad closed.

OperationCanceledException

cancellationToken was canceled.

ShowAdException

The activity is destroyed, no ad is loaded, or AdMob could not show it.

ArgumentOutOfRangeException

AdEarnedTimeout is out of its range, found once the ad has closed.