Home

Awesome

<h1 align="center"> <br> Jc.AdMob.Avalonia <br> &nbsp; </h1> <h4 align="center"> Library to bring AdMob advertisements to Avalonia mobile projects. </h4> <h6 align="center"> Avalonia solution derived from <a href="https://github.com/marius-bughiu/Plugin.AdMob/tree/main">marius-bughiu/Plugin.AdMob</a> </h6> <hr>

Table of Contents

Introduction

Jc.AdMob.Avalonia is a library to bring Google AdMob services to Avalonia Android and iOS projects.

The library currently supports the following ad units:

ConsentBannerInterstitialRewarded interstitialRewardedNative advancedApp open
Android
iOS

Usage

To use Jc.AdMob.Avalonia you must add the Jc.AdMob.Avalonia package to your cross-platform project.

dotnet add package Jc.AdMob.Avalonia

Followed by adding the Android/iOS Jc.AdMob.Avalonia.xxx package to the platform specific project(s).

dotnet add package Jc.AdMob.Avalonia.Android
dotnet add package Jc.AdMob.Avalonia.iOS

Then you must register AdMob in the CustomizeAppBuilder method in MainActivity.cs and AppDelegate.cs for Android and iOS respectively:

protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
    return base.CustomizeAppBuilder(builder)
        ...
        .UseAdMob(new AdMobOptions
        {
            TestDeviceIds = [],
            TagForUnderAgeOfConsent = false,
            TagForChildDirectedTreatment = false,
        });
}

Finally, follow the AdMob platform specific instructions in regard to configuring the application/unit ids.

Test Devices

To configure test devices, the .UseAdMob() method accepts a collection of test device ids.

Consent

Google requires all publishers serving ads to EEA and UK users to use a Google-Certified Consent Management Platform (CMP).

Jc.AdMob.Avalonia provides a consent service using the Google User Messaging Platform (UMP) SDK.

Usage

When using the consent service, all services are accessed via the singleton AdMob.Current.

To enable ads in regions where consent is required, you must show the consent dialog once consent is initialized:

AdMob.Current.Consent.OnConsentInitialized += (_, _) => AdMob.Current.Consent.ShowConsent();

Events

EventNotes
OnConsentInitialized
OnConsentFailedToInitialize
OnConsentFormLoadediOS only - issue being investigated
OnConsentFormFailedToLoadiOS only - issue being investigated
OnConsentFormFailedToPresent
OnConsentProvidedUser consented and ads can be shown

Banners

iOSAndroid
<img alt="iOS Banner" src="img/iOS Banner.png" width="250" /><img alt="iOS Banner" src="img/Android Banner.jpeg" width="250" />

The BannerAd control can be added to your XAML like so:

...
xmlns:admob="https://github.com/jc-admob-avalonia"
...

<admob:BannerAd UnitId="{ADMOB_UNIT_ID}" />

Sizes

The banner can be configured with the following sizes:

AdSizeSize
BannerMobile Marketing Association (MMA) banner ad size (320x50 density-independent pixels).
FullBannerInteractive Advertising Bureau (IAB) full banner ad size (468x60 density-independent pixels).
InvalidAn invalid AdSize that will cause the ad request to fail immediately.
LargeBannerLarge banner ad size (320x100 density-independent pixels).
LeaderboardInteractive Advertising Bureau (IAB) leaderboard ad size (728x90 density-independent pixels).
MediumRectangleInteractive Advertising Bureau (IAB) medium rectangle ad size (300x250 density-independent pixels).
WideSkyscrapperIAB wide skyscraper ad size (160x600 density-independent pixels).

Note: If the size is left unspecified, the banner will resize to fit the parent container.

Events

The banner exposes the following events:

EventNotes
OnAdLoaded
OnAdFailedToLoad
OnAdImpression
OnAdClicked
OnAdOpened
OnAdClosed
OnAdSwipedAndroid only

Interstitial

iOSAndroid
<img alt="iOS Banner" src="img/iOS Interstitial.png" width="250" /><img alt="iOS Banner" src="img/Android Interstitial.jpeg" width="250" />

Interstitial ads can be used by calling Interstitial.Create(unitId) on the AdMob singleton.

This call will load and the return the interstitial ad with an OnAdLoaded event once it's finished loaded. .Show() can then be called to display the ad.

public ICommand ShowInterstitialAdCommand { get; }

public MainViewModel()
{
    ShowInterstitialAdCommand = ReactiveCommand.Create(ShowInterstitialAd);
}

private void ShowInterstitialAd()
{
    var interstitial = AdMob.Current.Interstitial.Create();
    interstitial.OnAdLoaded += (_, _) => interstitial.Show();
}

Android

When using interstitial ads on Android, the Activity must be passed into the .AddAdMob() call:

protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
    return base.CustomizeAppBuilder(builder)
        ...
        .UseAdMob(this, adMobOptions);
}

Events

EventNotes
OnAdLoaded
OnAdFailedToLoad
OnAdPresented
OnAdFailedToPresent
OnAdImpression
OnAdClicked
OnAdClosed

Rewarded Interstitial

iOSAndroid
<img alt="iOS Banner" src="img/iOS Rewarded Interstitial.jpeg" width="250" /><img alt="iOS Banner" src="img/Android Rewarded Interstitial.jpg" width="250" />

Interstitial ads can be used by calling RewardedInterstitial.Create(unitId) on the AdMob singleton.

This call will load and the return the interstitial ad with an OnAdLoaded event once it's finished loaded. .Show() can then be called to display the ad.

The reward event OnUserEarnedReward can then be used to know when a reward can be given to the user.

public ICommand ShowRewardedInterstitialAdCommand { get; }

public MainViewModel()
{
    ShowRewardedInterstitialAdCommand = ReactiveCommand.Create(ShowRewardedInterstitialAd);
}

private void ShowRewardedInterstitialAd()
{
    var rewardedInterstitial = AdMob.Current.RewardedInterstitial.Create();
    rewardedInterstitial.OnAdLoaded += (_, _) => rewardedInterstitial.Show();
    rewardedInterstitial.OnUserEarnedReward += (_, reward) => Debug.WriteLine($"User earned reward: {reward.Amount} {reward.Type}");
}

Android

When using interstitial ads on Android, the Activity must be passed into the .AddAdMob() call:

protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
    return base.CustomizeAppBuilder(builder)
        ...
        .UseAdMob(this, adMobOptions);
}

Events

EventNotes
OnAdLoaded
OnAdFailedToLoad
OnAdPresented
OnAdFailedToPresent
OnAdImpression
OnAdClicked
OnAdClosed
OnUserEarnedRewardContains a RewardItem record

RewardItem

The reward item record is comprised of the following properties:

PropertyDescription
AmountThe amount rewarded.
TypeThe type of reward earned.

Rewarded

iOSAndroid
<img alt="iOS Banner" src="img/iOS Rewarded Interstitial.jpeg" width="250" /><img alt="iOS Banner" src="img/Android Rewarded Interstitial.jpg" width="250" />

Interstitial ads can be used by calling Rewarded.Create(unitId) on the AdMob singleton.

This call will load and the return the interstitial ad with an OnAdLoaded event once it's finished loaded. .Show() can then be called to display the ad.

The reward event OnUserEarnedReward can then be used to know when a reward can be given to the user.

public ICommand ShowRewardedAdCommand { get; }

public MainViewModel()
{
    ShowRewardedAdCommand = ReactiveCommand.Create(ShowRewardedAd);
}

private void ShowRewardedAd()
{
    var rewarded = AdMob.Current.Rewarded.Create();
    rewarded.OnAdLoaded += (_, _) => rewarded.Show();
    rewarded.OnUserEarnedReward += (_, reward) => Debug.WriteLine($"User earned reward: {reward.Amount} {reward.Type}");
}

Android

When using rewarded ads on Android, the Activity must be passed into the .AddAdMob() call:

protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
    return base.CustomizeAppBuilder(builder)
        ...
        .UseAdMob(this, adMobOptions);
}

Events

EventNotes
OnAdLoaded
OnAdFailedToLoad
OnAdPresented
OnAdFailedToPresent
OnAdImpression
OnAdClicked
OnAdClosed
OnUserEarnedRewardContains a RewardItem record

Troubleshooting

The Google Mobile Ads SDK was initialized without AppMeasurement

If you receive an error similar to the following:

GADInvalidInitializationException Reason: The Google Mobile Ads SDK was initialized without AppMeasurement. Google AdMob publishers, follow instructions here: https://googlemobileadssdk.page.link/admob-ios-update-plist to include the AppMeasurement framework and set the -ObjC linker flag. Google Ad Manager publishers, follow instructions here: https://googlemobileadssdk.page.link/ad-manager-ios-update-plist

Try adding the following to your Info.plist:

<key>GADIsAdManagerApp</key>
<true/>

The current iOS bindings are a little out of date and are still using an older version of the Google Mobile Ads SDK. While the version is now no longer a sunset version by Google, this is actively being worked on here.