marcojak / MauiMTAdmob

MIT License
104 stars 17 forks source link

The ad size and ad unit ID must be set before loadAd is called. #47

Closed MarcosCostaDev closed 6 months ago

MarcosCostaDev commented 6 months ago

I don't know if it's just me. But I have a routine that sets the AdId programmatically, and I am receiving this error

Java.Lang.IllegalStateException: 'The ad size and ad unit ID must be set before loadAd is called.'

this is the way I am setting the ads.

I am using the MVVM with the Maui Toolkit

public partial class MyViewModel : ObservableObject
{
    [ObservableProperty]
    private string _adsBanner; // I set this value when the navigation to the page is called

}
                        <admob:MTAdView
                            AdSize="Banner"
                            AdsId="{Binding AdsBanner}" />

My MauiProgram

  var builder = MauiApp.CreateBuilder();
  builder
      .UseMauiMTAdmob()
      .ConfigureFonts(fonts =>
      {
          fonts.AddFont("OpenSansRegular.ttf", "OpenSansRegular");
          fonts.AddFont("OpenSansSemibold.ttf", "OpenSansSemibold");
      });

This is some logs I am receiving

Loaded assembly: Anonymously Hosted DynamicMethods Assembly [External]
[DynamiteModule] Local module descriptor class for com.google.android.gms.ads.dynamite not found.
[DynamitePackage] Instantiating com.google.android.gms.ads.ChimeraAdManagerCreatorImpl
[Ads] This request is sent from a test device.
[DynamiteModule] Local module descriptor class for com.google.android.gms.ads.dynamite not found.
[Ads] Not retrying to fetch app settings
[EGL_emulation] eglCreateContext: 0xad295390: maj 2 min 0 rcv 2
[DynamiteModule] Considering local module com.google.android.gms.ads.dynamite:0 and remote module com.google.android.gms.ads.dynamite:234310602
[DynamiteModule] Selected remote version of com.google.android.gms.ads.dynamite, version >= 234310602
[EGL_emulation] eglCreateContext: 0xad296820: maj 2 min 0 rcv 2
[DynamiteModule] Considering local module com.google.android.gms.ads.dynamite:0 and remote module com.google.android.gms.ads.dynamite:234310602
[DynamiteModule] Selected remote version of com.google.android.gms.ads.dynamite, version >= 234310602
xplicit concurrent copying GC freed 75555(4111KB) AllocSpace objects, 58(3772KB) LOS objects, 49% free, 9184KB/17MB, paused 234us total 16.321ms
The thread 0x8f9c has exited with code 0 (0x0).
The thread 0x8dcc has exited with code 0 (0x0).
The thread 0x2c0 has exited with code 0 (0x0).
The thread 0x9578 has exited with code 0 (0x0).
**Java.Lang.IllegalStateException:** 'The ad size and ad unit ID must be set before loadAd is called.'

Version 1.0.2 was also tested in version 1.0.4 Project using net7.0-android

MarcosCostaDev commented 6 months ago

This issue has been fixed through a custom control.

public class AdvertisingControl : ContentView
{

    public static readonly BindableProperty AdsIdentifierProperty = BindableProperty.Create(nameof(AdsIdentifier), typeof(string), typeof(AdvertisingControl), null);
    public static readonly BindableProperty SizeProperty = BindableProperty.Create(nameof(Size), typeof(BannerSize), typeof(AdvertisingControl), BannerSize.Banner);

      public string AdsIdentifier
      {
          get => (string)GetValue(AdsIdentifierProperty);
          set {
              SetValue(AdIdentifierProperty, value);
          }
      }

      public BannerSize Size
      {
          get => (BannerSize)GetValue(SizeProperty);
          set => SetValue(SizeProperty, value);
      }

    public AdvertisingControl ()
    {
    }

      protected override void OnBindingContextChanged()
      {
          base.OnBindingContextChanged();

          if (string.IsNullOrWhiteSpace(AdsIdentifier)) return;

          var adView = new MTAdView { AdsId = AdsIdentifier, AdSize = Size};

          adView.LoadAd();

          Content = adView;
      }
  }

and I add it in any page that I need to show Ad Banner

        <controls:AdvertisingControl AdsIdentifier="ca-app-pub-3940256099942544/6300978111"  />