googleads / googleads-mobile-unity

Official Unity Plugin for the Google Mobile Ads SDK
https://developers.google.com/admob/unity
Apache License 2.0
1.35k stars 1.08k forks source link

OnUserReward Event is not invoking sometimes #1795

Closed mohammad7800 closed 1 year ago

mohammad7800 commented 2 years ago

Hi I'm using admob to monetize my game but there is small problem with using reward ads. reward ads are working perfectly in android but IOS users can't get rewards due to onUserReward Event is not Invoked most of the times. Please check this Issue Thanks

Other SDKs In project

SmartLook, Firebase, Admob, GameAnalytics, AppsFlyer

Relevant Code:

#define Test

using UnityEngine;
using GoogleMobileAds.Api;
using Firebase.Analytics;
using System;
using System.Collections.Generic;

public class AdmobScript : AdsManager
{
    //#if Test
    //    const string INTERSTETIAL_AD_ID = "ca-app-pub-3940256099942544/1033173712";
    //    const string BANNER_AD_ID = "ca-app-pub-3940256099942544/6300978111";
    //    const string REWARDED_AD_ID = "ca-app-pub-3940256099942544/5224354917";

#if UNITY_ANDROID
    const string INTERSTETIAL_AD_ID = "ca-app-pub-2825102185515786/1057008443";
    const string BANNER_AD_ID = "ca-app-pub-2825102185515786/3505425428";
    const string REWARDED_AD_ID = "ca-app-pub-2825102185515786/8566180419";
#elif UNITY_IPHONE
        const string INTERSTETIAL_AD_ID = "ca-app-pub-2825102185515786/2596626299";
        const string BANNER_AD_ID = "ca-app-pub-2825102185515786/7303540953";
        const string REWARDED_AD_ID = "ca-app-pub-2825102185515786/5782575722";
#else
        const string INTERSTETIAL_AD_ID = "unexpected_platform";
        const string BANNER_AD_ID = "unexpected_platform";
        const string REWARDED_AD_ID = "unexpected_platform";
#endif

    [SerializeField] private AdPosition bannerPosition = AdPosition.Bottom;

    private BannerView bannerView;
    private InterstitialAd interstitialAd;
    private RewardedAd rewardedAd;
    private Action onRewarded;

    public override bool IsBannerReady => true;

    public override bool IsInterstetialReady => interstitialAd.IsLoaded();

    public override bool IsRewardedReady => rewardedAd.IsLoaded();

    public override void Initialize()
    {
        MobileAds.Initialize(initStatus =>
        {
            //Dictionary<string, AdapterStatus> map = initStatus.getAdapterStatusMap();
            //foreach (KeyValuePair<string, AdapterStatus> keyValuePair in map)
            //{
            //    string className = keyValuePair.Key;
            //    AdapterStatus status = keyValuePair.Value;
            //    switch (status.InitializationState)
            //    {
            //        case AdapterState.NotReady:
            //            // The adapter initialization did not complete.
            //            Debug.Log("Adapter: " + className + " not ready.");
            //            break;
            //        case AdapterState.Ready:
            //            // The adapter was successfully initialized.
            //            Debug.Log("Adapter: " + className + " is initialized.");
            //            break;
            //    }
            //}
        });

#if Test
        var config = MobileAds.GetRequestConfiguration().ToBuilder()
            .SetTestDeviceIds(new List<string>() { "d981ea58c03f381a26b86cd9f24f1b28" }).build();
        MobileAds.SetRequestConfiguration(config);
#endif
        bannerView = new BannerView(BANNER_AD_ID, AdSize.Banner, bannerPosition);
        interstitialAd = new InterstitialAd(INTERSTETIAL_AD_ID);
        rewardedAd = new RewardedAd(REWARDED_AD_ID);
        AddListeners();
        FirebaseAnalytics.LogEvent("ad_initialization_complete_admob");
        Debug.Log("Admob Initialized");
        LoadBanner();
        LoadInterstetial();
        LoadRewarded();
    }

    private void AddListeners()
    {
        bannerView.OnAdLoaded += (x, o) =>
        {
            FirebaseAnalytics.LogEvent("ad_banner_loaded_admob");
            ShowBanner();
        };

        bannerView.OnAdFailedToLoad += (x, o) =>
        {
            FirebaseAnalytics.LogEvent("ad_banner_error_admob", new Parameter[] {
            new Parameter("message",o.LoadAdError.GetMessage()),
            });

            Debug.Log($"Banner Error: {o.LoadAdError.GetMessage()}");
        };

        interstitialAd.OnAdLoaded += (x, o) =>
        {
            Debug.Log("Interstitial Loaded");
            FirebaseAnalytics.LogEvent("ad_intrestitial_loaded_admob");
        };

        interstitialAd.OnAdFailedToLoad += (x, o) => FirebaseAnalytics.LogEvent("ad_intrestitial_failed_loading_admob");
        interstitialAd.OnAdFailedToShow += (x, o) => FirebaseAnalytics.LogEvent("ad_intrestitial_failed_showing_admob");
        interstitialAd.OnAdClosed += (x, o) => FirebaseAnalytics.LogEvent("ad_intrestitial_compelete_admob");
        rewardedAd.OnAdLoaded += (x, o) => FirebaseAnalytics.LogEvent("ad_reward_loaded_admob");
        rewardedAd.OnAdFailedToLoad += (x, o) => FirebaseAnalytics.LogEvent("ad_reward_failed_loading_admob");
        rewardedAd.OnAdFailedToShow += (x, o) => FirebaseAnalytics.LogEvent("ad_reward_failed_showing_admob");
        rewardedAd.OnUserEarnedReward += (x, o) =>
        {
            Debug.Log("Ad User Earned Reward 0");
            onRewarded?.Invoke();
            Debug.Log("Ad User Earned Reward 1");
            FirebaseAnalytics.LogEvent("ad_reward_compelete_admob");
        };

        Debug.Log("Ad Listeners Added");
    }

    public override void LoadBanner()
    {
        if (!ShowAds) return;
        Debug.Log("Ad Loading Banner");

        FirebaseAnalytics.LogEvent("ad_banner_request_admob");
        var request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request);
    }

    public override void ShowBanner()
    {
        if (!ShowAds) return;
        Debug.Log("Ad Showing Banner");

        bannerView.Show();
    }

    public override void LoadInterstetial()
    {
        if (!ShowAds) return;
        Debug.Log("Ad Loading Interstetial");

        FirebaseAnalytics.LogEvent("ad_intrestitial_request_admob");
        var request = new AdRequest.Builder().Build();
        interstitialAd.LoadAd(request);
    }

    public override void ShowInterstetial()
    {
        if (!ShowAds) return;
        Debug.Log("Ad Showing Interstetial");

        if (IsInterstetialReady)
        {
            interstitialAd.Show();
            LoadInterstetial();
            FirebaseAnalytics.LogEvent("ad_banner_shown_admob");
        }
    }

    public override void LoadRewarded()
    {
        Debug.Log("Ad Loading Rewarded");
        FirebaseAnalytics.LogEvent("ad_reward_request_admob");
        var request = new AdRequest.Builder().Build();
        rewardedAd.LoadAd(request);
    }

    public override void ShowRewarded(Action onReward)
    {
        Debug.Log("Ad Showing Rewarded");

        if (IsRewardedReady)
        {
            onRewarded = onReward;
            rewardedAd.Show();
            LoadRewarded();
            FirebaseAnalytics.LogEvent("ad_reward_shown_admob");
        }
    }
}
yerfdogj commented 2 years ago

Possibly related to this issue?: https://github.com/googleads/googleads-mobile-unity/issues/976#issuecomment-558753013 That is, AdMob events don't necessarily call back to the handlers using the Main Unity thread.

dgogolev commented 2 years ago

Hi, I have the same issue. Did you managed this problem somehow?

yerfdogj commented 2 years ago

Hi, I have the same issue. Did you managed this problem somehow?

We happen to be using UniRx, so we used their MainThreadDispatcher method in the event handler hookup, like this: _rewardedAd.OnUserEarnedReward += (object sender, Reward e) => MainThreadDispatcher.Post(_ => RewardedVideoAdRewardedEvent(sender, e), null);

NVentimiglia commented 1 year ago

Unable to replicate. Please try again and let us know if you still have issues.