can't work the RegisterEventHandlers function on real device.

222 views
Skip to first unread message

Kevin Soh

unread,
Jul 20, 2023, 3:18:14 AM7/20/23
to Google Mobile Ads SDK Developers
Dear eveybody.
I have a question for interstitial ad.
it seems to work well in Unity Editor but it doesn't seem to run on real device.
I don't know cause.
please check this....

void Start(){
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
        LoadInterstitialAd(); // load the  interstitial ad.
    }
 private void OnCollisionEnter2D(Collision2D collision) {
       
        rb.velocity = Vector2.zero;
        Invoke("sceneChange", 1.0f); // 
   
    }
private void sceneChange(){
        gameCount++;
        if(gameCount <= 3){
            SceneManager.LoadScene("GameOver",LoadSceneMode.Single);
        }else{
            gameCount = 0;
            ShowAd();
        }
  }

public void LoadInterstitialAd()
    {
        // Clean up the old ad before loading a new one.
        if (interstitialAd != null)
        {
                interstitialAd.Destroy();
                interstitialAd = null;
        }

        //Debug.Log("Loading the interstitial ad.");

        // create our request used to load the ad.
        var adRequest = new AdRequest();
        adRequest.Keywords.Add("unity-admob-sample");
       
        // send the request to load the ad.
        InterstitialAd.Load(_adUnitId, adRequest,
            (InterstitialAd ad, LoadAdError error) =>
            {
                // if error is not null, the load request failed.
                if (error != null || ad == null)
                {
                    Debug.LogError("interstitial ad failed to load an ad " +
                                    "with error : " + error);
                    return;
                }
                interstitialAd = ad;
            });
           
            RegisterEventHandlers(interstitialAd); --> call the event.
    }

 public void ShowAd()
    {
        if (interstitialAd != null && interstitialAd.CanShowAd())
        {
            interstitialAd.Show();
        }
        else
        {
            Debug.LogError("Interstitial ad is not ready yet.");
        }
    }

private void RegisterEventHandlers(InterstitialAd ad)
    {
        // Raised when the ad is estimated to have earned money.
        ad.OnAdPaid += (AdValue adValue) =>
        {
            Debug.Log(String.Format("Interstitial ad paid {0} {1}.", adValue.Value, adValue.CurrencyCode));
        };
        // Raised when an impression is recorded for an ad.
        ad.OnAdImpressionRecorded += () =>
        {
            Debug.Log("Interstitial ad recorded an impression.");
        };
        // Raised when a click is recorded for an ad.
        ad.OnAdClicked += () =>
        {
            Debug.Log("Interstitial ad was clicked.");
        };
        // Raised when an ad opened full screen content.
        ad.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("Interstitial ad full screen content opened.");
        };
        // Raised when the ad closed full screen content.
        ad.OnAdFullScreenContentClosed += () =>
        {
             Debug.Log("Interstitial ad full screen content Closed.");
             sceneChange();
           };
        // Raised when the ad failed to open full screen content.
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Interstitial ad failed to open full screen content " +
                        "with error : " + error);
        };
    }

my phone information.
Adroid version : 13
One UI version : 5.1

Best Regards!
Kevin.




Mobile Ads SDK Forum Advisor

unread,
Jul 20, 2023, 5:53:03 AM7/20/23
to ehs...@gmail.com, google-adm...@googlegroups.com
Hi Kevin,

Thank you for reaching out to us. Looking into your code snippet, I can confirm that your implementation of the interstitial is fine. I went ahead and tried our samples (https://github.com/googleads/googleads-mobile-unity/tree/main/samples) and I was able to trigger the RegisterEventHandlers. With that, could you please try/confirm the following?
  • Try our sample app and let us know if you still encounter the same
  • What SDK version are you using?
  • Could you try to export the project first into Android Studio, then run your app from there instead? 
This message is in relation to case "ref:_00D1U1174p._5004Q2n9nkC:ref"

Thanks,
 
Google Logo Mobile Ads SDK Team


Reply all
Reply to author
Forward
0 new messages