AdMob - Callback Handlers are not firing (Unity 5.3.3p3 - Android)

1,049 views
Skip to first unread message

Michal Krlín

unread,
Mar 20, 2016, 7:56:31 PM3/20/16
to Google Mobile Ads SDK Developers
Good Day, i have problems with AdMob callback handlers an im getting mad from that! :O
Callback Handlers are not firing, for example this:
 //c#
void HandleAdClosing(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleAdClosing event received");
   
}
does nothing...
 However requesting, showing, hiding and deleting of Interstitial and Banner ads is working fine
Im using Unity 5.3.3p3 , Official Google Mobile Ads Unity Plugin v3.0.3, and Lunar Console plugin. 
In AndroidManifests of both plugins I repaired Min and Max SDK Version...
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="23" />

Then I followed instructions hereIm using script similar to official demo script, see below. Lunar Console is not writing any Debug log in event handlers. Tested on LG G3 (Android 6.0) and tablet Sencor ELEMENT 7Q001 (Android 4.4). Results both same, nothing fired. It must be some really stupid reason. Please help, and sorry for my english... Thanks 


//c#
using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;




// Example script showing how to invoke the Google Mobile Ads Unity plugin.
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
   
public Camera Cam;
   
private BannerView bannerView;
   
private InterstitialAd interstitial;
 
   
private float deltaTime = 0.0f;
   
private static string outputMessage = "";


   
public static string OutputMessage
   
{
       
set { outputMessage = value; }
   
}


 
   
void Update()
   
{
       
       
// Calculate simple moving average for time to render screen. 0.1 factor used as smoothing
       
// value.
        deltaTime
+= (Time.deltaTime - deltaTime) * 0.1f;
   
}


   
void OnGUI()
   
{
       
GUIStyle style = new GUIStyle();


       
Rect rect = new Rect(0, 0, Screen.width, Screen.height);
        style
.alignment = TextAnchor.LowerRight;
        style
.fontSize = (int)(Screen.height * 0.06);
        style
.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
       
float fps = 1.0f / deltaTime;
       
string text = string.Format("{0:0.} fps", fps);
        GUI
.Label(rect, text, style);


       
// Puts some basic buttons onto the screen.
        GUI
.skin.button.fontSize = (int)(0.03f * Screen.height);


       
Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height,
                                     
0.8f * Screen.width, 0.1f * Screen.height);
       
if (GUI.Button(requestBannerRect, "Request Banner"))
       
{
           
RequestBanner();
       
}


       
Rect showBannerRect = new Rect(0.1f * Screen.width, 0.175f * Screen.height,
                                 
0.8f * Screen.width, 0.1f * Screen.height);
       
if (GUI.Button(showBannerRect, "Show Banner"))
       
{
            bannerView
.Show();
       
}


       
Rect destroyBannerRect = new Rect(0.1f * Screen.width, 0.3f * Screen.height,
                                     
0.8f * Screen.width, 0.1f * Screen.height);
       
if (GUI.Button(destroyBannerRect, "Destroy Banner"))
       
{
            bannerView
.Destroy();
       
}


       
Rect requestInterstitialRect = new Rect(0.1f * Screen.width, 0.425f * Screen.height,
                                           
0.8f * Screen.width, 0.1f * Screen.height);
       
if (GUI.Button(requestInterstitialRect, "Request Interstitial"))
       
{
           
RequestInterstitial();
       
}


       
Rect showInterstitialRect = new Rect(0.1f * Screen.width, 0.55f * Screen.height,
                                       
0.8f * Screen.width, 0.1f * Screen.height);
       
if (GUI.Button(showInterstitialRect, "Show Interstitial"))
       
{
           
ShowInterstitial();
       
}


     
       
Rect textOutputRect = new Rect(0.1f * Screen.width, 0.925f * Screen.height,
                                 
0.8f * Screen.width, 0.05f * Screen.height);
        GUI
.Label(textOutputRect, outputMessage);
   
}


   
private void RequestBanner()
   
{
       
#if UNITY_EDITOR
           
string adUnitId = "unused";
       
#elif UNITY_ANDROID
       
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
       
#elif UNITY_IPHONE
       
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
       
#else
           
string adUnitId = "unexpected_platform";
       
#endif


       
// Create a 320x50 banner at the top of the screen.
        bannerView
= new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
       
// Register for ad events.
        bannerView
.OnAdLoaded += HandleAdLoaded;
        bannerView
.OnAdFailedToLoad += HandleAdFailedToLoad;
        bannerView
.OnAdLoaded += HandleAdOpened;
        bannerView
.OnAdClosed += HandleAdClosed;
        bannerView
.OnAdLeavingApplication += HandleAdLeftApplication;
       
// Load a banner ad.
        bannerView
.LoadAd(createAdRequest());
   
}


   
private void RequestInterstitial()
   
{
       
#if UNITY_EDITOR
           
string adUnitId = "unused";
       
#elif UNITY_ANDROID
       
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
       
#elif UNITY_IPHONE
       
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
       
#else
           
string adUnitId = "unexpected_platform";
       
#endif


       
// Create an interstitial.
        interstitial
= new InterstitialAd(adUnitId);
       
// Register for ad events.
        interstitial
.OnAdLoaded += HandleInterstitialLoaded;
        interstitial
.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
        interstitial
.OnAdOpening += HandleInterstitialOpened;
        interstitial
.OnAdClosed += HandleInterstitialClosed;
        interstitial
.OnAdLeavingApplication += HandleInterstitialLeftApplication;
       
// Load an interstitial ad.
        interstitial
.LoadAd(createAdRequest());
   
}


   
// Returns an ad request with custom ad targeting.
   
private AdRequest createAdRequest()
   
{
       
return new AdRequest.Builder()
               
.AddTestDevice(AdRequest.TestDeviceSimulator)
                 
.AddTestDevice("16564E08A2534032")
               
.AddKeyword("game")
               
.SetGender(Gender.Male)
               
.SetBirthday(new DateTime(1985, 1, 1))
               
.TagForChildDirectedTreatment(false)
               
.AddExtra("color_bg", "9B30FF")
               
.Build();
   
}


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


 
   
#region Banner callback handlers


   
public void HandleAdLoaded(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleAdLoaded event received.");
   
}


   
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleFailedToReceiveAd event received with message: " + args.Message);
   
}


   
public void HandleAdOpened(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleAdOpened event received");
   
}


   
void HandleAdClosing(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleAdClosing event received");
   
}


   
public void HandleAdClosed(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleAdClosed event received");
   
}


   
public void HandleAdLeftApplication(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleAdLeftApplication event received");
   
}


   
#endregion


   
#region Interstitial callback handlers


   
public void HandleInterstitialLoaded(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleInterstitialLoaded event received.");
   
}


   
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleInterstitialFailedToLoad event received with message: " + args.Message);
   
}


   
public void HandleInterstitialOpened(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleInterstitialOpened event received");
   
}


   
void HandleInterstitialClosing(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleInterstitialClosing event received");
   
}


   
public void HandleInterstitialClosed(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleInterstitialClosed event received");
   
}


   
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
   
{
       
Cam.backgroundColor = Color.red;
       
Debug.Log("HandleInterstitialLeftApplication event received");
   
}


   
#endregion


 
}


Veer Arjun Busani

unread,
Mar 21, 2016, 12:17:19 PM3/21/16
to Google Mobile Ads SDK Developers
Hi Michal,

Am I missing something here? In your Interstitial Ad-
 // Create an interstitial.
        interstitial = new InterstitialAd(adUnitId);
        // Register for ad events.
        interstitial.OnAdLoaded += HandleInterstitialLoaded;
        interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
        interstitial.OnAdOpening += HandleInterstitialOpened;
        interstitial.OnAdClosed += HandleInterstitialClosed;
        interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
        // Load an interstitial ad.
I do not see that you're calling HandleAdClosing. Instead you are using this on Banner Ads.  For Banner Ads, onAdClosed() would only be called if a clickthrough opens an In-App browser full screen Ad. But let us know if you are able to listen to all other callbacks.
 
Thanks,
Veer Busani
Mobile Ads SDK Team

Michal Krlín

unread,
Mar 21, 2016, 4:50:45 PM3/21/16
to Google Mobile Ads SDK Developers
Hi, thanks for reply. 
I modified my requests voids like this:
 private void RequestBanner()
    {
        #if UNITY_EDITOR
            string adUnitId = "unused";
        #elif UNITY_ANDROID
string adUnitId = "ca-app-pub-2456842918997107/5030646679";
        #elif UNITY_IPHONE
string adUnitId = "ca-app-pub-2456842918997107/5030646679";
        #else
            string adUnitId = "unexpected_platform";
        #endif

        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
        // Register for ad events.

        bannerView.OnAdLoaded += HandleAdLoaded;
        bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
bannerView.OnAdOpening += HandleAdOpening;
//bannerView.OnAdOpened += ... ; //Causes error: `GoogleMobileAds.Api.InterstitialAd' does not contain a definition for `OnAdOpened' and no extension method `OnAdOpened' of type `GoogleMobileAds.Api.InterstitialAd' could be found
        bannerView.OnAdClosed += HandleAdClosed;
        bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
        // Load a banner
        bannerView.LoadAd(createAdRequest());
    }

    private void RequestInterstitial()
    {
        #if UNITY_EDITOR
            string adUnitId = "unused";
        #elif UNITY_ANDROID
string adUnitId = "ca-app-pub-2456842918997107/3722037079";
        #elif UNITY_IPHONE
string adUnitId = "ca-app-pub-2456842918997107/3722037079";
        #else
            string adUnitId = "unexpected_platform";
        #endif

        // Create an interstitial.
        interstitial = new InterstitialAd(adUnitId);
        // Register for ad events.
        interstitial.OnAdLoaded += HandleInterstitialLoaded;
        interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.OnAdOpening += HandleInterstitialOpening;
//interstitial.OnAdOpened += ... ; //Causes error: `GoogleMobileAds.Api.InterstitialAd' does not contain a definition for `OnAdOpened' and no extension method `OnAdOpened' of type `GoogleMobileAds.Api.InterstitialAd' could be found
        interstitial.OnAdClosed += HandleInterstitialClosed;
        interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;        
// Load an interstitial ad.
        interstitial.LoadAd(createAdRequest());
    }


and handlers voids like this:
   #region Banner callback handlers

    public void HandleAdLoaded(object sender, EventArgs args)
    {

Cam.backgroundColor = Color.red;
        Debug.Log("HandleAdLoaded event received.");
    }

    public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
Cam.backgroundColor = Color.red;
        Debug.Log("HandleFailedToReceiveAd event received with message: " + args.Message);
    }

    public void HandleAdOpening(object sender, EventArgs args)
    {
Cam.backgroundColor = Color.red;
        Debug.Log("HandleAdOpened event received");
    }

    void HandleAdClosing(object sender, EventArgs args)
    {
Cam.backgroundColor = Color.red;
        Debug.Log("HandleAdClosing event received");
    }

    public void HandleAdClosed(object sender, EventArgs args)
    {
Cam.backgroundColor = Color.red;
        Debug.Log("HandleAdClosed event received");
    }

    public void HandleAdLeftApplication(object sender, EventArgs args)
    {
Cam.backgroundColor = Color.red;
        Debug.Log("HandleAdLeftApplication event received");
    }

    #endregion

    #region Interstitial callback handlers

    public void HandleInterstitialLoaded(object sender, EventArgs args)
    {
Cam.backgroundColor = Color.red;
        Debug.Log("HandleInterstitialLoaded event received.");
    }

    public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
Cam.backgroundColor = Color.red;
        Debug.Log("HandleInterstitialFailedToLoad event received with message: " + args.Message);
    }

    public void HandleInterstitialOpening(object sender, EventArgs args)
    {
Cam.backgroundColor = Color.red;
        Debug.Log("HandleInterstitialOpening event received");
    }

    public void HandleInterstitialClosed(object sender, EventArgs args)
    {
Cam.backgroundColor = Color.red;
        Debug.Log("HandleInterstitialClosed event received");
    }

    public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    {
Cam.backgroundColor = Color.red;
        Debug.Log("HandleInterstitialLeftApplication event received");
    }

    #endregion

But sadly nothing changed.  None of the callbacks is not working...

Veer Arjun Busani

unread,
Mar 21, 2016, 5:24:18 PM3/21/16
to Google Mobile Ads SDK Developers
Hi Michal,

Your code snippet does seem to be in place. But I would ask you to try and test with the demo script that I have attached just for sanity check. It might be due to Debug.Log(). Do get back to us if this does not work for you.
GoogleMobileAdsDemoScript.cs

Michal Krlín

unread,
Mar 21, 2016, 5:48:41 PM3/21/16
to Google Mobile Ads SDK Developers
I tried with your demo script (whitch is completly without debug logs), sadly the same results... 

Michal Krlín

unread,
Mar 21, 2016, 6:02:08 PM3/21/16
to Google Mobile Ads SDK Developers
Im attaching my entire project. You can try build and run on device, two finger swipe down will run LunarConsole.
HelloWorld.rar
Message has been deleted

Veer Arjun Busani

unread,
Mar 22, 2016, 10:52:44 AM3/22/16
to Google Mobile Ads SDK Developers
Hi Michal,

I have just now tested your sample project and I was successfully able to receive callbacks. I would like you to recheck the project. Make sure the sample script is attached as a component to the main camera and run. Even on the Unity Editor, you should be able to receive callbacks.

Michal Krlín

unread,
Mar 22, 2016, 12:16:31 PM3/22/16
to Google Mobile Ads SDK Developers
Thats very strange,its still not working for me. LunarConsole is still empty, do you see messages from callback handlers in LunarConsole? Is possible that i have problems with my external tools? I will try update Android SDK, JDK and JDR. 

Veer Arjun Busani

unread,
Mar 22, 2016, 12:55:18 PM3/22/16
to Google Mobile Ads SDK Developers
Hi Michal,

I would not be able provide support for any third party frameworks such as LunarConsole. I was able to receive callbacks on the default Unity Console which meant there is no issue with the Mobile Ads SDK itself.

I have attached a screenshot for the callbacks that I have received.
Screen Shot 2016-03-22 at 12.54.24 PM.png

Michal Krlín

unread,
Mar 22, 2016, 6:14:13 PM3/22/16
to Google Mobile Ads SDK Developers
Ok, after few days i maybe finaly figured it out. It was because really stupid reason:

1) I must have unticked Development build and Script Debugging in Build Settings.

2) LunarConsole is not possible to write print() in callback voids. That confused me, however LunarConsole is not the problem why event handlers has not been fired.

Anyway, thank you for your efforts, I writed on 3 forums and you are the only person who replayed to me. :)
Reply all
Reply to author
Forward
0 new messages