ADMob How too, A FULL EXAMPLE

For a VERY long time a bunch of people have been asking how to integrate Admob into there Android MonoGame Apps, After traveling around the Web and trying MANY examples, Most in Java and All NEVER a complete guide, I’ve completed the simplest and FULL example on how to do the simplest way possible using Visual Studio 2017 and the Latest Monogame 3.6 Dev. Here it is.

Firstly after creating your Android App Open “Manage NuGet Packages” (Right click project->Manage NuGet Packages)
Browse search for this “Xamarin.GooglePlayServices.Ads.Lite” and install it

if its a new Project, create an Android manifest (Right Click project->Prooperties->Android Manifest->Create)
Insert the following via editing the Android Manifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
	<!-- Below MUST be put into the manifest to allow the ad to react to the user-->
	<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

Heres and example of MY FULL Manifest

xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AdController.AdController" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
	<uses-sdk android:targetSdkVersion="22" android:minSdkVersion="22" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
	<!-- Below MUST be put into the manifest to allow the ad to react to the user-->
	<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
	<application android:label="AdController"></application>
</manifest>

Next Make a new Class Call it what ever you want, I called Mine AdController my NameSpace/Project was also called AdController so don’t forget to change the namespace to yours

Here is the complete code for the Class

using Android.Gms.Ads;
using Android.Gms.Ads.Reward;

/// <summary>
/// For Ads to work 
/// NuGet Xamarin.GooglePlayServices.Ads.Lite
/// Only then can ads be added to the project
/// 
/// AndroidManifest.xml File
/// add these lines
// 	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//	  <uses-permission android:name="android.permission.INTERNET" />
//	<!-- Below MUST be put into the manifest to allow the ad to react to the user-->
//	  <activity android:name="com.google.android.gms.ads.AdActivity"
//	  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
///
/// </summary>

namespace AdController
{
  public static class AdController
  {
    public static InterstitialAd interstitialHandle = null;
    public static IRewardedVideoAd rewardHandle = null;


    // APP ID This is a test use yours from Admob
    private static string appID = "ca-app-pub-3940256099942544~3347511713";
    // AD ID's This is a test use yours from Admob
    public static string adID1 = "ca-app-pub-3940256099942544/1033173712"; // standard full page ad
    public static string adID2 = "ca-app-pub-3940256099942544/5224354917"; // reward full page ad

    public static bool adRegularLoaded = false;
    public static bool adRewardLoaded = false;
    public static bool adRewarded = false;
    public static bool youMadeMoney = false;

    /*********************************************************************************************
     * Function Name : InitAdRegularAd
     * Description : Initialize a regular full page ad
     * if you need to change the adID, just set
     * interstitialRegular.AdUnitId to point to another adIDx
     * ******************************************************************************************/
    public static void InitRegularAd()
    {
      MobileAds.Initialize(Game1.Activity,appID);                                               // initialize the ads        
      interstitialHandle = new InterstitialAd((Activity1)Game1.Activity);
      interstitialHandle.AdUnitId = adID1;                                                     // adID string, can repoint this on the fly
      interstitialHandle.AdListener = null;
      ListeningRegular listening = new ListeningRegular();
      interstitialHandle.AdListener = listening;
      interstitialHandle.LoadAd(new AdRequest.Builder().Build());
    }
    /*********************************************************************************************
     * Function Name : ShowRegularAd
     * Description : display the ad
     * ******************************************************************************************/
    public static void ShowRegularAd()
    {
      if(adRegularLoaded)
      {
        adRegularLoaded = false;
        adRewarded = false;
        interstitialHandle.Show();
      }
    }
    /*********************************************************************************************
     * Function Name : InitRewardAd
     * Description : Initialize a reward Ad
     * ******************************************************************************************/
    public static void InitRewardAd()
    {
      ListeningReward listening = new ListeningReward();                                          // create a pointer to our listen class      
      rewardHandle = MobileAds.GetRewardedVideoAdInstance(Game1.Activity);                        // initialize the handle      
      rewardHandle.UserId = appID;                                                                // set the App ID      
      rewardHandle.RewardedVideoAdListener = listening;                                           // point to the rewards Listen class      
      rewardHandle.LoadAd(adID2,new AdRequest.Builder().Build());                                 // load the first one      
    }
    /*********************************************************************************************
     * Function Name : ShowRewardAd
     * Description : display the ad
     * ******************************************************************************************/
    public static void ShowRewardAd()
    {
      if(adRewardLoaded)                                                                          // we ready?        
      {
        adRewardLoaded = false;                                                                   // reset triggers        
        adRewarded = false;
        rewardHandle.Show();                                                                      // show the ad        
      }
    }
  }
  /*********************************************************************************************
    * Class Name : ListeningRegular
    * Description : Listening class for the regualr ad
    * ******************************************************************************************/
  internal class ListeningRegular : AdListener
  {
    public override void OnAdLoaded()
    {
      AdController.adRegularLoaded = true;
      base.OnAdLoaded();
    }
    public override void OnAdClosed()
    {
      // load the next ad ready to display
      AdController.interstitialHandle.LoadAd(new AdRequest.Builder().Build());
      base.OnAdClosed();
    }
    public override void OnAdOpened()
    {
      base.OnAdOpened();
    }
  }
  /*********************************************************************************************
    * Function Name : ListeningReward
    * Description : Listening class for the reward ad
    * ******************************************************************************************/
  internal class ListeningReward : AdListener, IRewardedVideoAdListener
  {
    public ListeningReward()
    {
      // constructor, up to you if you need to do anything in here
    }
    public void OnRewarded(IRewardItem reward)
    {
      // you can use the reward variable to handle your own rewards
      AdController.adRewarded = true;
    }
    public void OnRewardedVideoAdClosed()
    {
      // load the next ad ready to display
      AdController.rewardHandle.LoadAd(AdController.adID2,new AdRequest.Builder().Build());
    }
    public void OnRewardedVideoAdFailedToLoad(int errorCode)
    {
      // error message if you want will be ignored otherwise
    }
    public void OnRewardedVideoAdLeftApplication()
    {
      // ad was clicked on so you just made money
      AdController.youMadeMoney = true;
    }
    public void OnRewardedVideoAdLoaded()
    {
      // ad was loaded
      AdController.adRewardLoaded = true;
    }
    public void OnRewardedVideoAdOpened()
    {
      // ad opening
    }
    public void OnRewardedVideoStarted()
    {
      //ad running
    }
  }
}

And Here is how I called it as an example from MonoGame Game1.cs

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
private SpriteFont font;
protected override void LoadContent()
{
  // Create a new SpriteBatch, which can be used to draw textures.
  spriteBatch = new SpriteBatch(GraphicsDevice);
  font = Content.Load<SpriteFont>("debugFont");

  // GET AD'S READY TO BE DISPLAYED
  AdController.InitRegularAd();
  AdController.InitRewardAd();

  // TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
  // TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
  if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  {
    Exit();
  }

  // TODO: Add your update logic here

  base.Update(gameTime);
}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
/// 
double oldCount = 0;
Random rnd = new Random();
protected override void Draw(GameTime gameTime)
{
  GraphicsDevice.Clear(Color.CornflowerBlue);

  // TODO: Add your drawing code here
  spriteBatch.Begin();
  if(oldCount < gameTime.TotalGameTime.TotalMilliseconds)
  {
    oldCount = gameTime.TotalGameTime.TotalMilliseconds + 3000;
    if((rnd.Next() & 1) == 1)
    {
      if(AdController.adRewardLoaded)
      {
        AdController.ShowRewardAd();
      }
    }
    else
    {
      if(AdController.adRegularLoaded)
      {
        AdController.ShowRegularAd();
      }
    }
  }
  else
    spriteBatch.DrawString(font,"Waiting for an Ad Reward from ad="+AdController.adRewarded,new Vector2(340,300),Color.White);
  spriteBatch.End();

  base.Draw(gameTime);
   }
 }

That’s it all done, A complete class to display Full page Ad’s from Inside MonoGame including rewards and all there triggers
If you have any comments lets me know, don’t forget to get an AdMob account from www.AdMob.com and use your ID’s in the code above.
Happy Coding,
Enjoy
Paul

4 Likes

Thanks for posting this Paul!
I bundled this up into a library and pushed it up to Nuget to make it even easier to add AdMob to MonoGame projects:

Cheers!

@dmanning23
The sample isn’t working. You can find the issue here: https://github.com/dmanning23/RevMobBuddySample/issues/1

Awesome guide!

But for me it only seems to show test ads. If I add my admob IDs it doesn’t show apps. I double checked the app and ad IDs are correct. Any ideas?

If your app is showing the test ads and the IDs are correct, then you are good to go.
It can take several days for ads to be added to your hopper. Check back on the app in like a week.

I found the issue with mine, in case it helps anyone else.

In admob site I deactivated that it use the location in advanced settings (my app doesn’t have location permission).

Then it worked.

1 Like

got this error on line 119 of your code in AdController.cs:

‘ListeningReward’ does not implement interface member ‘IRewardedVideoAdListener.OnRewardedVideoCompleted()’

Dont know what to do.