Reward ad error on mobile devices

119 views
Skip to first unread message

Roberto Flores

unread,
Dec 5, 2022, 4:47:36 PM12/5/22
to Google Mobile Ads SDK Developers
I followed the steps provided to load and display a reward ad. In unity, it works fine. However, on mobile, I am getting the following error: "Tried to show rewarded ad before it was ready. This should in theory never happen. If it does, please contact the plugin owners." Not sure how to resolve this. Any help would be appreciated.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;

public class RewardAdManager : MonoBehaviour
{
    private RewardedAd rewardedAd;
    public UnityEvent OnAdLoadedEvent;
    public UnityEvent OnAdFailedToLoadEvent;
    public UnityEvent OnAdOpeningEvent;
    public UnityEvent OnAdFailedToShowEvent;
    public UnityEvent OnUserEarnedRewardEvent;
    public UnityEvent OnAdClosedEvent;

    public Text rewardText;
    private int gameAmount = 0;
    private int rewardAmount;
    private string idApp, idRewardAd;

    public static RewardAdManager instance;

    void Awake(){
        instance = this;
    }

    void Start(){
        List<String> deviceIds = new List<String>() { AdRequest.TestDeviceSimulator };

        idApp = "75EF8D155528C04DACBBA6F36F433035";
        idRewardAd = "ca-app-pub-3940256099942544/5224354917";

        // Add some test device IDs (replace with your own device IDs).
        #if UNITY_EDITOR
            deviceIds.Add("unused");
        #elif UNITY_ANDROID
            deviceIds.Add(idApp);
        #else
            deviceIds.Add("unexpected_platform");
        #endif

        // Configure TagForChildDirectedTreatment and test device IDs.
        RequestConfiguration requestConfiguration =
            new RequestConfiguration.Builder()
            .SetTagForChildDirectedTreatment(TagForChildDirectedTreatment.Unspecified)
            .SetTestDeviceIds(deviceIds).build();
        MobileAds.SetRequestConfiguration(requestConfiguration);

        //Initialize the Google Mobile Ads SDK.
        //MobileAds.Initialize(HandleInitCompleteAction);
    }

    void Update(){
        if(rewardAmount > 0){
            gameAmount = rewardAmount;
            rewardText.text = "Points Earned: " + gameAmount.ToString();
            rewardAmount=0;
        }
    }

    /*private void HandleInitCompleteAction(InitializationStatus initstatus) {
        Debug.Log("Initialization complete.");

        MobileAdsEventExecutor.ExecuteInUpdate(() => {
            RequestAndLoadRewardedAd();
        });
    }*/

    public void RequestAndLoadRewardedAd() {
        Debug.Log("Requesting Rewarded ad.");

        //create new rewarded ad instance
        rewardedAd = new RewardedAd("ca-app-pub-3940256099942544/5224354917");

        // Add Event Handlers
        rewardedAd.OnAdLoaded += (sender, args) =>
        {
            Debug.Log("Reward ad loaded.");
            OnAdLoadedEvent.Invoke();
        };
        rewardedAd.OnAdFailedToLoad += (sender, args) =>
        {
            Debug.Log("Reward ad failed to load.");
            OnAdFailedToLoadEvent.Invoke();
        };
        rewardedAd.OnAdOpening += (sender, args) =>
        {
            Debug.Log("Reward ad opening.");
            OnAdOpeningEvent.Invoke();
        };
        rewardedAd.OnAdFailedToShow += (sender, args) =>
        {
            Debug.Log("Reward ad failed to show with error: "+ args.AdError.GetMessage());
            OnAdFailedToShowEvent.Invoke();
        };
        rewardedAd.OnAdClosed += (sender, args) =>
        {
            Debug.Log("Reward ad closed.");
            OnAdClosedEvent.Invoke();
        };
        rewardedAd.OnUserEarnedReward += (sender, args) =>
        {
            Debug.Log("User earned Reward ad reward: "+ args.Amount);
            rewardAmount = (int) args.Amount;
            OnUserEarnedRewardEvent.Invoke();
        };
        rewardedAd.OnAdDidRecordImpression += (sender, args) =>
        {
            Debug.Log("Reward ad recorded an impression.");
        };

        //Create empty ad request and show once loaded
        rewardedAd.LoadAd(CreateAdRequest());
    }

    public void ShowRewardedAd() {
        if (rewardedAd != null) {
            rewardedAd.Show();
        } else {
            Debug.Log("Rewarded ad is not ready yet.");
        }
    }

   /* public void MobileRewardAd(){
        StartCoroutine(Delay());
    }

    IEnumerator Delay(){
        RequestAndLoadRewardedAd();
        yield return new WaitForSeconds(2f);
        ShowRewardedAd();
    }*/

    private AdRequest CreateAdRequest() {
        return new AdRequest.Builder().AddKeyword("ca-app-pub-3940256099942544/5224354917").Build();
    }

    public void DestroyRewardAd() {
        if (rewardedAd != null) {
            rewardedAd.Destroy();
        }
    }
}

Mobile Ads SDK Forum Advisor

unread,
Dec 6, 2022, 1:41:09 AM12/6/22
to rbrtfl...@gmail.com, google-adm...@googlegroups.com

Hi Roberto,

 

Thank you for reaching out to us.

 

For us to further check this behavior you've experience, can you provide us the following details below privately? You can provide the following details via reply privately to author option or directly provide it to the link below.

 

  • Sample app project where the behavior is reproducible
  • Steps to replicate
  • SDK version
  • App ID
  • Device name and versions affected

 

If the file(s) you are looking to share are less than 25mb in total you can attach them to this case on your next reply. If you are having trouble attaching your file to this case or if your file(s) are larger than 25mb, you can share your files with me by performing the following steps:

 

1. Navigate to

https://docs.google.com/forms/d/e/1FAIpQLSfkAiXMeYP-fw1W3Z-tT9uwmATEKO5X6S-th0gR2ezdKaaqfg/viewform?usp=pp_url&entry.400550049=Mobile+Ads+SDK&entry.460850823=5004Q00002giw0eQAA&entry.80707362=00159580

2. Fill out all fields, and attach your file(s).

3. Please reply back on this thread when you have uploaded your file(s). Please do not share this link.

 

Regards,

Google Logo
Princess Pamela
Mobile Ads SDK Team
 


ref:_00D1U1174p._5004Q2giw0e:ref
Reply all
Reply to author
Forward
0 new messages