xamarin / GooglePlayServicesComponents

Other
316 stars 147 forks source link

Problems with the implementation of Interstitial ads #496

Open brdacon1 opened 3 years ago

brdacon1 commented 3 years ago

Hello everyone,

Please help me i don't know how to go about solving this case

After 120.0.0 updates I couldn't implement Interstitial ads in my Xamarin.Android project it returns errors in the following implementation:

using Android.Gms.Ads; using Android.Gms.Ads.Interstitial; using OF.Controls; using OF.Droid; using Xamarin.Forms;

[assembly: Dependency(typeof(AdInterstitial_Droid))]

namespace OF.Droid { public class AdInterstitial_Droid : IAdInterstitial { InterstitialAd interstitialAd;

    public AdInterstitial_Droid()
    {
        interstitialAd = new InterstitialAd(Android.App.Application.Context);

        // TODO: change this id to your admob id  
        interstitialAd.AdUnitId = "ca-app-pub-54";
        LoadAd();
    }

    void LoadAd()
    {
        var requestbuilder = new AdRequest.Builder();
        interstitialAd.LoadAd(requestbuilder.Build());
    }

    public void ShowAd()
    {
        if (interstitialAd.IsLoaded)
            interstitialAd.Show();

        LoadAd();
    }
}

}

brdacon1 commented 3 years ago

Hello, Someone can helpe me?

eduardoqc183 commented 3 years ago

Try this. `public class InterstitialAdsImplementation : IAdmobInterstitialAds { static InterstitialAd mInterstitialAd;

    public void Display(string adId)
    {
        try
        {
            AdRequest adRequest = new AdRequest.Builder().Build();
            InterstitialCallback.LaodInterstitial(Android.App.Application.Context, adId, adRequest, new InterstitialCallback());
            if (mInterstitialAd != null)
            {
                mInterstitialAd.Show(CrossCurrentActivity.Current.Activity);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    public class InterstitialCallback : Android.Gms.Ads.AdLoadCallback
    {
        public override void OnAdLoaded(Java.Lang.Object p0)
        {
            mInterstitialAd = (InterstitialAd)p0;
            base.OnAdLoaded(p0);
        }

        public override void OnAdFailedToLoad(LoadAdError p0)
        {
            base.OnAdFailedToLoad(p0);
        }

        public static void LaodInterstitial(Android.Content.Context context, string Adunit, Android.Gms.Ads.AdRequest adRequest, Android.Gms.Ads.AdLoadCallback Callback)
        {
            IntPtr AdRequestClass = JNIEnv.GetObjectClass(adRequest.Handle);
            IntPtr zzdsmethodID = JNIEnv.GetMethodID(AdRequestClass, "zza", "()Lcom/google/android/gms/internal/ads/zzbdq;");
            Java.Lang.Object zzzk = GetObject<Java.Lang.Object>(JNIEnv.CallObjectMethod(adRequest.Handle, zzdsmethodID), JniHandleOwnership.TransferLocalRef);

            IntPtr zzakjtype = JNIEnv.FindClass("com/google/android/gms/internal/ads/zzbof");
            IntPtr zzakjConstructor = JNIEnv.GetMethodID(zzakjtype, "<init>", "(Landroid/content/Context;Ljava/lang/String;)V");
            IntPtr zzakjInstance = JNIEnv.NewObject(zzakjtype, zzakjConstructor, new JValue[] { new JValue(context), new JValue(new Java.Lang.String(Adunit)) });

            IntPtr LoadMethodId = JNIEnv.GetMethodID(zzakjtype, "zza", "(Lcom/google/android/gms/internal/ads/zzbdq;Lcom/google/android/gms/ads/AdLoadCallback;)V");
            JNIEnv.CallVoidMethod(zzakjInstance, LoadMethodId, new JValue[] { new JValue(zzzk), new JValue(Callback) });
        }
    }
}`
mhdwaelanjo commented 1 year ago

I fixed the problem.. we can use my libraries :

 <PackageReference Include="Anjo.Android.GoogleServices.Ads" Version="22.0.0" />
 <PackageReference Include="Anjo.Android.GoogleServices.AdsBase" Version="22.0.0" />
 <PackageReference Include="Anjo.Android.GoogleServices.AdsLite" Version="22.0.0" />

Supports fixing the problem of Loading ads using :