Admob Ads do not show up (XamarinForms + C# + Android)

I'm trying to show a bottom banner on my App but I can't make it show up. Funny that it was working just fine but after a few weeks without coding it just stopped working and not even test codes from Google show up. Sometimes it works after a compilation and when I unplug the phone and close/open the App again it stop showing the ads again.

I also used the same Admob code into a Unity quiz game I created and over there it works fine everytime.

PS: I'm a bit new to Xamarin and C#, this is my first App.

Here's the relevant code:


MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DDP"
x:Class="DDP.MainPage"
BackgroundColor="#3f183d"
Title="My App Title">

<StackLayout BackgroundColor="Transparent" HeightRequest="70" HorizontalOptions="Start" VerticalOptions="Center" WidthRequest="1000">
    <local:AdMobView x:Name="adMobView" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand"/>
</StackLayout>
</ContentPage>

MainPage.xaml.cs

    public MainPage()
    {
        InitializeComponent();

        BindingContext = this;
        adMobView.AdUnitId = AdMobView.codigoAdmob;
    }

AdMobView.cs

using Xamarin.Forms;

namespace DDP
{
    public class AdMobView : View
{

public static readonly BindableProperty AdUnitIdProperty = BindableProperty.Create(
    nameof(AdUnitId),
    typeof(string),
    typeof(AdMobView),
    string.Empty);

public string AdUnitId
{
    get => (string)GetValue(AdUnitIdProperty);
    set => SetValue(AdUnitIdProperty, value);
}

//admob google test code
public static string codigoAdmob = "ca-app-pub-3940256099942544/6300978111";

}

}


AdMobViewRenderer.cs

using System.ComponentModel;
using DDP;
using DDP.Droid;
using Android.Content;
using Android.Gms.Ads;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(AdMobView), typeof(AdMobViewRenderer))]
namespace DDP.Droid
{
    public class AdMobViewRenderer : ViewRenderer<AdMobView, AdView>
    {
        public AdMobViewRenderer(Context context) : base(context) { }

        protected override void         OnElementChanged(ElementChangedEventArgs<AdMobView> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null && Control == null)
        {
            SetNativeControl(CreateAdView());
        }
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == nameof(AdView.AdUnitId))
            Control.AdUnitId = Element.AdUnitId;
    }

    private AdView CreateAdView()
    {
        var adView = new AdView(Context)
        {
            AdSize = AdSize.SmartBanner,
            AdUnitId = Element.AdUnitId
        };

        adView.LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);

        adView.LoadAd(new AdRequest.Builder().Build());

        return adView;
    }

}

}


MainActivity.cs

//somecode

base.OnCreate(bundle);
MobileAds.Initialize(ApplicationContext, "ca-app-pub-3940256099942544/6300978111");
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());

//somecode

AndroidManifest.xml


Other info

I'm using 'Xamarin.GooglePlayServices.Ads.Lite' NuGet package and I checked permissions to 'Network_State' and 'Internet'

Thanks !

1 answer

  • answered 2018-11-16 23:47 SushiHangover

    Filter by the TAG of Ads and you will see different Information and/or Warning log entries concerning AdMob. These will range from timed outs and load failures (usually related to no Internet access) to not refreshing the ad since it is not currently visible in the UI (but an AdView instance was created), etc...

    Example:

    adb logcat -s Ads
    

    Example Output (Not a complete list):

    I Ads     : Starting ad request.
    I Ads     : SDK version: XXXXXXXXXX
    I Ads     : This request is sent from a test device.
    I Ads     : Scheduling ad refresh 70000 milliseconds from now.
    I Ads     : Ad is not visible. Not refreshing ad.
    W Ads     : There was a problem getting an ad response. ErrorCode: 0
    W Ads     : Failed to load ad: 0
    W Ads     : Not retrying to fetch app settings
    W Ads     : Invoke Firebase method getInstance error.
    W Ads     : The Google Mobile Ads SDK will not integrate with Firebase. Admob/Firebase integration requires the latest Firebase SDK jar, but Firebase SDK is either missing or out of date
    W Ads     : App does not have the required permissions to get location
    W Ads     : Timed out waiting for ad response.
    W Ads     : Failed to load ad: 2