2

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.

Dave
  • 2,684
  • 2
  • 21
  • 38
rony_y
  • 535
  • 1
  • 8
  • 26

2 Answers2

2

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();
    }
}
Dave
  • 2,684
  • 2
  • 21
  • 38
  • Greetings to all tried the answer provided above and a all bunch of things including re installing the unity package and Android studio all together if anyone knows how to resolve this issue it will be much appreciated like before iOS still works Android does not – rony_y May 04 '19 at 21:19
  • Are you sure you have correct app and ad ids? I have tested it and it works fine. – Dave May 04 '19 at 21:40
  • Hey @Dave yes i copied the app id to th Android manifest and to the .cs script and like i stated iOS works just fine can you give me the process you used? – rony_y May 04 '19 at 21:59
  • 1
    Change your android appId and adUnitId in your script to test ids provided by admob: "ca-app-pub-3940256099942544~3347511713" and "ca-app-pub-3940256099942544/6300978111". Let me know if you get the test banner ad. – Dave May 04 '19 at 22:34
  • Hey Dave with the test ad it works on Android now how can i make it work with actual ads? – rony_y May 05 '19 at 13:26
  • 1
    Hi. You must be giving wrong android app and android banner id for production. Make sure you copy right ids from your admob console. Be careful with real ads, if you want to use real production ids you need to set your device as a test device otherwise you are risking your account blocked. You can read more here: [enable test devices](https://developers.google.com/admob/unity/test-ads#enable_test_devices) – Dave May 05 '19 at 13:44
  • Hey Dave thank you very much for your help i copied the app id and banner id as is i cant really tell whats wrong with the prod version of it ill try again thanks a lot. – rony_y May 05 '19 at 13:52
  • Het i tried again with Android verified appId and bannerId are copied exactly as list in my account like stated before iOS works Android test Ad works but Android actual ads still dont work i also ran from Unity Android resolver which fixed the test ads issue but not real ads. – rony_y May 06 '19 at 19:19
  • 1
    Is your app published? I am not sure but maybe google is preventing "live" ads on your device with your google account. I would try with a different device, with different account and different network when the app is published. Everything else seems to be setup properly. You can check for errors with OnAdFailedToLoad and adb logcat. – Dave May 06 '19 at 19:40
  • My app isnt published maybe this is the issue thank you everyone for you help. – rony_y May 07 '19 at 18:04
  • 1
    Ok the issue was like Dave stated the app needed to be published first in the Google Play Store i published the app (before exporting an APK i ran Android rsolver from Unity Assets -> Play service Resolve -> Android Resolver -> Force resolver) then exported an APK uploaded to the Google play store now ads appear thank you all for your help. – rony_y May 08 '19 at 14:20
  • Thank you for the of adding the HandleOnAdLoaded event. I did not find that in google/unity documentation – Christian Rios Oct 15 '19 at 00:14
1

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

Kavita Patil
  • 1,784
  • 1
  • 17
  • 30
  • I have updated my answer, please refer the link to check your AdMob code with test IDs – Kavita Patil Apr 30 '19 at 19:11
  • Hello thanks for your response i tried all of the above still no go its been almost 2 weeks and like i said on iOS it works just fine. – rony_y Apr 30 '19 at 19:17