Unity adMobs Android not displaying bannerView

1.4k Views Asked by At

I am trying to integrate AdMobs into my unity 2D project (game is designed for mobile platforms).

After searching the web and answers here i could not find the solution to my problem.

  • When i port my game to each platform iOS works and displays the banner view.

  • Android does not display the banner view.

  • I tried deleting and re importing the google package and still Android wont show the banner.

I did exactly what the google tutorial in this link describes. https://developers.google.com/admob/unity/start

But still no go Android won't display the banner view with the ad (tested on 2 seperate devices). Here is my code i added the appId string to both manifest and plist handlers in the project.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System;

public class GoogleAdsHandler:MonoBehaviour {

    private BannerView bannerView;

    // Use this for initialization
    void Start () {
#if UNITY_ANDROID
                string appId = Consts.ANDROID_AD_APPID;
#elif UNITY_IPHONE
        string appId = Consts.IOS_AD_APPID;
#else
                string appId = "unexpected_platform";
#endif
        InitilizeAdMob ();
    }

    private void InitilizeAdMob () {
#if UNITY_ANDROID
        string appId = Consts.ANDROID_AD_APPID;
#elif UNITY_IPHONE
        string appId = Consts.IOS_AD_APPID;
#else
        string appId = "unexpected_platform";
#endif
        MobileAds.Initialize (appId);

        this.RequestBanner ();
    }

    private void RequestBanner () {
#if UNITY_ANDROID
        string adUnitId = Consts.ANDROID_BANNER_ID;
#elif UNITY_IPHONE
        string adUnitId = Consts.IOS_BANNER_ID;
#else
            string adUnitId = "unexpected_platform";
#endif
        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView (adUnitId, AdSize.Banner, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder ().Build ();
        bannerView.LoadAd (request);
        bannerView.Show ();
        bannerView.OnAdLoaded += HandleOnAdLoaded;
    }

    public void HandleOnAdLoaded (object sender, EventArgs args) {
        MonoBehaviour.print ("HandleAdLoaded event received");
    }
}

This script is attached to a game object on my main menu scene.

Would appreciate help with the matter.

Kindest regards.

Rony.

2

There are 2 best solutions below

12
Dave On BEST ANSWER

You are trying to show the add without checking if the ad is loaded and it propably isn't. You should subscribe to HandleOnAdLoaded event before calling bannerView.LoadAd() and bannerView.Show().

Your Start method is just calling the InitilizeAdMob as the string your are assigning is not passed anywhere and you are doing the same thing in InitilizeAdMob so I would rewrite your class like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System;

public class GoogleAdsHandler : MonoBehaviour {

    private BannerView bannerView;

    // Use this for initialization
    void Start () {
       InitilizeAdMob ();
    }

    private void InitilizeAdMob () {
#if UNITY_ANDROID
        string appId = Consts.ANDROID_AD_APPID;
#elif UNITY_IPHONE
        string appId = Consts.IOS_AD_APPID;
#else
        string appId = "unexpected_platform";
#endif
        MobileAds.Initialize (appId);
        this.RequestBanner ();
    }

    private void RequestBanner () {
#if UNITY_ANDROID
        string adUnitId = Consts.ANDROID_BANNER_ID;
#elif UNITY_IPHONE
        string adUnitId = Consts.IOS_BANNER_ID;
#else
        string adUnitId = "unexpected_platform";
#endif
        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView (adUnitId, AdSize.Banner, AdPosition.Bottom);
        bannerView.OnAdLoaded += HandleOnAdLoaded;
        AdRequest request = new AdRequest.Builder ().Build ();
        bannerView.LoadAd (request);
    }

    public void HandleOnAdLoaded (object sender, EventArgs args) {
        MonoBehaviour.print ("HandleAdLoaded event received");
        bannerView.Show();
    }
}
2
Kavita Patil On

Here are some common causes:

  1. Make sure you have updated AdMob with your payment details
  2. Make sure that the ads you created in AdMob are banner ads.
  3. Check your AdMob dashboard to see the status of your ads, are they active?
  4. Verify you used the correct Ad Unit Id.
  5. Give it 24 hours, it can take time for an ad to become active in your region

You can also refer the test IDs to check your adMOb code, provided by Google: https://developers.google.com/admob/android/test-ads