3

I am working on an iOS app with Unity 2019.2.21f1. I set up an adMob account about two months ago, and had gotten banner ads and interstitial ads to show successfully in my app shortly after that.

Then, about a week ago, the ads stopped showing up. I'm not sure if I unwittingly changed something in my code/admob configuration, or if there is something else going on here...

This is my script for managing AdMob ads:

using System;
using UnityEngine;
using GoogleMobileAds.Api;



public class AdBannerScriptAdMob : MonoBehaviour
{
    private BannerView bannerView;
    private InterstitialAd interstitialAd;
    private MainPlayer mainPlayer;
    int previousMod;

    public bool bHasRequestedInterstitialAd;
    public bool bHasShownInterstitialAd;
    public bool bHasActiveNoAdsSubcription;


    public void Start()
    {
        bHasRequestedInterstitialAd = false;
        bHasShownInterstitialAd = false;
        bHasActiveNoAdsSubcription = false;
        mainPlayer = GameObject.FindObjectOfType<MainPlayer>();
#if UNITY_ANDROID
        string appId = "ca-app-pub-3940256099942544~3347511713";
#elif UNITY_IPHONE
        string appId = "ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx";
#else                   
        string appId = "unexpected_platform";
#endif

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        this.RequestBanner();
    }

    private AdRequest CreateAdRequest()
    {
        return new AdRequest.Builder()
        .AddKeyword("game")
        .SetGender(Gender.Male)
        .SetBirthday(new DateTime(1985, 1, 1))
        .TagForChildDirectedTreatment(false)
        .AddExtra("color_bg", "9B30FF")
        .Build();
    }


    public void Update()
    {

       // Debug.Log("printing mainPlayer.iNumBalls from AdBannerScriptAdMob: " + mainPlayer.iNumBalls);
        if ((mainPlayer.iNewNumBalls+10) %20 == 0 && !bHasActiveNoAdsSubcription)
        {
            if (!bHasRequestedInterstitialAd)
            { 
                Debug.Log("Should be requesting InterstitialAd now");
                RequestInterstitial();
                bHasRequestedInterstitialAd = true;
                bHasShownInterstitialAd = false;
            }
        }
        if ((mainPlayer.iNumBalls) % 20 < previousMod)
        {

            if (!bHasShownInterstitialAd && !bHasActiveNoAdsSubcription)
            {
                Debug.Log("Trying to show InterstitialAd now");
                this.ShowInterstitial();
                bHasShownInterstitialAd = true;
                bHasRequestedInterstitialAd = false;
            }
        }
        previousMod = (int)mainPlayer.iNumBalls % 20;

    }

    private void ShowInterstitial()
    {
        if (this.interstitialAd.IsLoaded())
        {
            Debug.Log("InterstitialAd is ready and should be showing now");
            this.interstitialAd.Show();
        }
        else
        {
            Debug.Log("Interstitial is not ready yet");

        }
    }
    private void RequestInterstitial()
    {
        // These ad units are configured to always serve test ads.
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"; //real ID from my admob account
#else
        string adUnitId = "unexpected_platform";
#endif

        // Clean up interstitial ad before creating a new one.
        if (this.interstitialAd != null)
        {
            this.interstitialAd.Destroy();
        }

        // Create an interstitial.
        this.interstitialAd = new InterstitialAd(adUnitId);

        // Register for ad events.
        this.interstitialAd.OnAdLoaded += this.HandleInterstitialLoaded;
        this.interstitialAd.OnAdFailedToLoad += this.HandleInterstitialFailedToLoad;
        this.interstitialAd.OnAdOpening += this.HandleInterstitialOpened;
        this.interstitialAd.OnAdClosed += this.HandleInterstitialClosed;
        this.interstitialAd.OnAdLeavingApplication += this.HandleInterstitialLeftApplication;

        // Load an interstitial ad.
        this.interstitialAd.LoadAd(this.CreateAdRequest());
    }

    private void RequestBanner()
    {
#if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"; //real ID from my admob account
#else 
        string adUnitId = "unexpected_platform";
#endif

        //AdSize adSize = new AdSize(250, 250);

        //adSize = AdSize.Banner;
        AdSize adSize = AdSize.SmartBanner;
        // Create a banner at the bottom of the screen.

        this.bannerView = new BannerView(adUnitId, adSize, AdPosition.Bottom);

        // Called when an ad request has successfully loaded.
        this.bannerView.OnAdLoaded += this.HandleOnAdLoaded;
        // Called when an ad request failed to load.
        this.bannerView.OnAdFailedToLoad += this.HandleOnAdFailedToLoad;
        // Called when an ad is clicked.
        this.bannerView.OnAdOpening += this.HandleOnAdOpened;
        // Called when the user returned from the app after an ad click.
        this.bannerView.OnAdClosed += this.HandleOnAdClosed;
        // Called when the ad click caused the user to leave the application.
        this.bannerView.OnAdLeavingApplication += this.HandleOnAdLeavingApplication;



        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();

        // Load the banner with the request.
        this.bannerView.LoadAd(request);
    }

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

    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "
                        + args.Message);
    }

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

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

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


#region Interstitial callback handlers

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

    public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
}

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

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

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

#endregion



}

When running the app via Xcode on my physical iOS device (iPhone 8, iOS 13.3.1), I get the following messages in the Xcode console:

HandleFailedToReceiveAd event received with message: Failed to receive ad with error: (null)

and

HandleInterstitialFailedToLoad event received with message: Failed to receive ad with error: (null)

I have verified and reverified that string appId (in method Start()) and both instances of string adUnitId in the methods RequestInterstitial() and RequestBanner() match the IDs from my adMob account. My adMob account shows the following status:

enter image description here

At this point, I am unsure what caused the ads to stop showing up. Internet connectivity on the mobile device is verified to be working.

Additional note: After having written this question, and before posting it, I tested my application repeatedly, and weirdly enough, one time, the interstitial did show up correctly. However, the remaining ~100 times, the interstitial did not show up at all, as described in this post. The banner ad still has not shown up at all. To my mind, this doesn't make the possible issue any clearer, but maybe it sheds some light on the issue for someone else?

ChrisC
  • 892
  • 2
  • 12
  • 33

0 Answers0