Admob ads wont show.

229 views
Skip to first unread message

Benjaman Harre

unread,
Jan 4, 2019, 2:15:55 PM1/4/19
to Google Mobile Ads SDK Developers
In the unity editor, I get the console prompt that a dummy ad was shown when using the buttons however on the device when I click the button to show a rewarded ad nothing happens and it will break the game meaning no other buttons or functions will work after. Here is my code can you tell me what I am doing wrong here.

A few months back I had test ads working fine but I guess there was a SDK update and now I cant even get test ads to load.

Thanks in advance.

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

public class AdManager : MonoBehaviour
{
    private InterstitialAd interstitial;
    private RewardBasedVideoAd rewardBasedVideo;

    private string adType;
    public Button coins, rubies, upgrade, powerup;
    public Text coolDown;

    private DateTime currentDate, oldDate;
    private TimeSpan timeAway, difference;
    private bool countDown;

    public GameObject msgPanel;
    public Text msgTitle;
    public Text msgBody;

    // Use this for initialization
    void Start()
    {
        #if UNITY_ANDROID
        string appId = "ca-app-pub-***************~***********"; // Edited out my real appId for this post.
        #elif UNITY_IPHONE
        string appId = "unexpected_platform";
        #else
        string appId = "unexpected_platform";
        #endif

        //MobileAds.SetiOSAppPauseOnBackground(true);

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

        // Get singleton reward based video ad reference.
        rewardBasedVideo = RewardBasedVideoAd.Instance;

        // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
        this.rewardBasedVideo.OnAdLoaded += this.HandleRewardBasedVideoLoaded;
        this.rewardBasedVideo.OnAdFailedToLoad += this.HandleRewardBasedVideoFailedToLoad;
        this.rewardBasedVideo.OnAdOpening += this.HandleRewardBasedVideoOpened;
        this.rewardBasedVideo.OnAdStarted += this.HandleRewardBasedVideoStarted;
        this.rewardBasedVideo.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
        this.rewardBasedVideo.OnAdClosed += this.HandleRewardBasedVideoClosed;
        this.rewardBasedVideo.OnAdLeavingApplication += this.HandleRewardBasedVideoLeftApplication;

        RequestRewardBasedVideo();
    }

    void Update()
    {
        CheckTime();

        if (countDown)
        {
            CountDown();
        }
    }

    // Returns an ad request with custom ad targeting.
    private AdRequest CreateAdRequest()
    {
        return new AdRequest.Builder().Build();
    }

    private void RequestInterstitial()
    {
        // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/8691691433";
#elif UNITY_IPHONE
        string adUnitId = "unexpected_platform";
#else
        string adUnitId = "unexpected_platform";
#endif

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

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

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

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

    private void RequestRewardBasedVideo()
    {
        #if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/5224354917";
        #elif UNITY_IPHONE
        string adUnitId = "unexpected_platform";
        #else
        string adUnitId = "unexpected_platform";
        #endif

        rewardBasedVideo.LoadAd(CreateAdRequest(), adUnitId);
    }

    private void ShowInterstitial()
    {
        if (interstitial.IsLoaded())
        {
            interstitial.Show();
        }
        else
        {
            MonoBehaviour.print("Interstitial is not ready yet");
        }
    }

    private void ShowRewardBasedVideo()
    {
        if (rewardBasedVideo.IsLoaded())
        {
            rewardBasedVideo.Show();
        }
        else
        {
            msgPanel.SetActive(true);
            msgTitle.text = "No ads ready!";
            msgBody.text = "Sorry, There is not an ad ready for you at this time.. Try again!";

            MonoBehaviour.print("Reward based video ad is not ready yet");
        }
    }

    public void PlayRewardedVideo(string type)
    {
        adType = type;
        Debug.Log("Attempting to fire reward ad request... IsLoaded = " + rewardBasedVideo.IsLoaded().ToString());

        ShowRewardBasedVideo();
    }

    #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

    #region RewardBasedVideo callback handlers

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

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

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

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

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

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;
        MonoBehaviour.print(
            "HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);

        if (type == "double")
        {
            Debug.Log("Double Coins! Working");
        }
        else if (type == "freecoins")
        {
            Debug.Log("Free Coins! Working");
            Player.GiveCoins(1000);
            SetDateTime();
            UnlockButton(coins, rubies);
            PlayerPrefs.SetString("CoinButton", "Locked");
        }
        else if (type == "freerubies")
        {
            Debug.Log("Free Rubies! Working");
            Player.GiveRubies(5);
            UnlockButton(rubies, upgrade);
            PlayerPrefs.SetString("RubiesButton", "Locked");
        }
        else if (type == "freehydros")
        {
            Debug.Log("Free Hydros! Working");
            Player.AddHydros(1);
            UnlockButton(upgrade, powerup);
        }
        else if (type == "freenitros")
        {
            Debug.Log("Free Nitros! Working");
            Player.AddNitros(1);
            UnlockButton(powerup, null);
        }
    }

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

    #endregion

    private void CheckTime()
    {
        //Store the current time when it starts
        currentDate = System.DateTime.Now;

        //Grab the old time from the player prefs as a long
        long temp = Convert.ToInt64(PlayerPrefs.GetString("videoRewards"));
        oldDate = DateTime.FromBinary(temp);

        //Use the Subtract method and store the result as a timespan variable
        difference = currentDate.Subtract(oldDate);
        timeAway = difference;
    }

    private void SetDateTime()
    {
        PlayerPrefs.SetString("videoRewards", DateTime.Now.ToBinary().ToString());
    }

    private void CheckUnlock()
    {
        if (timeAway >= TimeSpan.FromHours(24))
        {
            countDown = false;
            UnlockButtons();
            coolDown.text = "Your daily rewards are ready to be claimed!";
        }
        else
        {
            countDown = true;

            if (PlayerPrefs.GetString("CoinButton") == "Locked")
            {
                coins.interactable = false;
            }
            else
            {
                coins.interactable = true;
            }

            if (PlayerPrefs.GetString("RubiesButton") == "Locked")
            {
                rubies.interactable = false;
            }
            else
            {
                rubies.interactable = true;
            }

            if (PlayerPrefs.GetString("UpgradeButton") == "Locked")
            {
                upgrade.interactable = false;
            }
            else
            {
                upgrade.interactable = true;
            }

            if (PlayerPrefs.GetString("PowerupButton") == "Locked")
            {
                powerup.interactable = false;
            }
            else
            {
                powerup.interactable = true;
            }
        }
    }

    private void CountDown()
    {
        TimeSpan remaining = TimeSpan.FromHours(24) - timeAway;
        string hours = string.Format("{0}", remaining.Hours);
        string mins = string.Format("{0}", remaining.Minutes);
        string secs = string.Format("{0}", remaining.Seconds);

        if (remaining.Hours > 0)
        {
            if (coolDown != null)
                coolDown.text = "Daily Rewards Unlock in " + hours + " hours, " + mins + " minutes, and " + secs + " seconds!";
        }
        else if (remaining.Minutes > 0)
        {
            if (coolDown != null)
                coolDown.text = "Daily Rewards Unlock in " + mins + " minutes, and " + secs + " seconds!";
        }
        else
        {
            if (coolDown != null)
                coolDown.text = "Daily Rewards Unlock in " + secs + " seconds!";
        }
    }

    private void UnlockButtons()
    {
        coins.interactable = true;
        rubies.interactable = false;
        upgrade.interactable = false;
        powerup.interactable = false;

        PlayerPrefs.SetString("CoinButton", "Unlocked");
        PlayerPrefs.SetString("RubiesButton", "Unlocked");
        PlayerPrefs.SetString("UpgradeButton", "Unlocked");
        PlayerPrefs.SetString("PowerupButton", "Unlocked");
    }

    public void UnlockButton(Button cbtn, Button nbtn)
    {
        Debug.Log("UnlockButton Called");
        Debug.Log(cbtn);
        Debug.Log(nbtn);

        if (cbtn != null)
            cbtn.interactable = false;

        if (nbtn != null)
            nbtn.interactable = true;
    }
}


mobileadssdk-a...@google.com

unread,
Jan 4, 2019, 7:10:36 PM1/4/19
to Benjaman Harre, Google Mobile Ads SDK Developers
Hello Benjamin, 

Could you please confirm if you're using the latest version of the Unity package from here? If not, can you try importing the latest package and see if that works for you. Additionally, could you please confirm if you're having trouble loading the ads or receiving an error while compiling? If you could share us the complete device logs along with the Ad Unit IDs, so I can take a look? You may also refer to our Unity sample app which might be helpful with the implementation. 

Thanks,
Bharani Cherukuri
Mobile Ads SDK Team

=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+ page:
    http://googleadsdeveloper.blogspot.com
    https://plus.google.com/115658573333388777174/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

Benjaman Harre

unread,
Jan 5, 2019, 3:36:02 PM1/5/19
to Google Mobile Ads SDK Developers
I will go ahead and update the SDK just in case. The game will compile fine and when ran inside the Unity editor and I test the buttons to fire rewarded ads I get the following console printouts.

Dummy IsLoaded
UnityEngine.Debug:Log(Object)
GoogleMobileAds.Common.DummyClient:IsLoaded() (at Assets/GoogleMobileAds/Common/DummyClient.cs:147)
GoogleMobileAds.Api.RewardBasedVideoAd:IsLoaded() (at Assets/GoogleMobileAds/Api/RewardBasedVideoAd.cs:137)
AdManager:ShowRewardBasedVideo() (at Assets/AdManager.cs:135)
AdManager:PlayRewardedVideo(String) (at Assets/AdManager.cs:170)
UnityEngine.EventSystems.EventSystem:Update()

Dummy ShowRewardBasedVideoAd
UnityEngine.Debug:Log(Object)
GoogleMobileAds.Common.DummyClient:ShowRewardBasedVideoAd() (at Assets/GoogleMobileAds/Common/DummyClient.cs:183)
GoogleMobileAds.Api.RewardBasedVideoAd:Show() (at Assets/GoogleMobileAds/Api/RewardBasedVideoAd.cs:143)
AdManager:ShowRewardBasedVideo() (at Assets/AdManager.cs:141)
AdManager:PlayRewardedVideo(String) (at Assets/AdManager.cs:170)
UnityEngine.EventSystems.EventSystem:Update()


When the apk is compiled and running on my android device (Note 9) the game loads and works fine until I try to click the button for the reward video ad. At that time nothing happens and the game lock and not let any other buttons function.
As far as device logs. I am unsure on how to obtain them. I have attempted to run my apk on Android Studios Virtual Device but it wont install on it but will on my phone.

Benjaman Harre

unread,
Jan 6, 2019, 10:11:42 PM1/6/19
to Google Mobile Ads SDK Developers
I updated the SDK and recompiled. Same results


--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads...@googlegroups.com.
To post to this group, send email to google-adm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-admob-ads-sdk/fb1809c9-958f-42cd-a220-0d66b403061a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Benjaman Harre
(812) 568-6815 - Android

"Life is short, break the rules, forgive quickly, kiss slowly, love truly, laugh uncontrollably, and never regret anything that made you smile."

-Mark Twain-

mobileadssdk-a...@google.com

unread,
Jan 7, 2019, 2:58:24 PM1/7/19
to Benjaman Harre, Google Mobile Ads SDK Developers
Hello Benjamin, 

Thank you for the details. The Fiddler logs shared are does not have information about the error. Could you please share with us the Charles Proxy logs so I can share it with the team for further investigation? If you're having trouble capturing the Charles Proxy logs, could you get us the Network tracing logs from your device? This information will help us troubleshoot this. 

Thanks,
Bharani Cherukuri
Mobile Ads SDK Team

access...@gmail.com

unread,
Feb 18, 2019, 2:29:11 AM2/18/19
to google-adm...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages