Test ads are not being displayed in editor or in build

I’m currently trying to implement interstitial ads for android. I’m using the Advertisement Legacy package and version is 4.4.2.
Here is my code:

using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using UnityEditor;
using UnityEngine.Advertisements;
using UnityEngine;

public class PlayerController : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{

    ...

    // ad variables
    public string gameIdAndroid = "5367732";
    public string gameIdIOS = "5367733";
    public string adUnitIdAndroid = "Interstitial_Android";
    public string adUnitIdIOS = "Interstitial_iOS";
    public string adUnitId;
    public bool adStarted;
    private bool testMode = true;

    private void Start()
    {
       
...
     
        Advertisement.Initialize(gameIdAndroid, testMode);
        adUnitId = adUnitIdAndroid;
    }

    ...

    public void KillPlayer()
    {

        ...

        if (deaths % 2 == 0)
        {
            canMove = false;
            PlayAd();
        }
    }
    
...

    void PlayAd()
    {
            if (Advertisement.isInitialized && !adStarted)
            {
                Debug.Log("ad loading");
                Advertisement.Load(adUnitId);
            }
    }

    public void OnUnityAdsShowComplete(string adUnitIdc, UnityAdsShowCompletionState showCompletionState)
    {
        Debug.Log("ad completed");
        if (showCompletionState == UnityAdsShowCompletionState.COMPLETED)
        {
            canMove = true;
            adStarted = false;
            Debug.Log("ad completed");
        }
        else if (showCompletionState == UnityAdsShowCompletionState.SKIPPED)
        {
            canMove = true;
            adStarted = false;
            Debug.Log("ad skipped");
        }
    }

    public void OnUnityAdsFailedToLoad(string _adUnitId, UnityAdsLoadError error, string message)
    {
        Debug.Log($"Error loading Ad Unit: {_adUnitId} - {error.ToString()} - {message}");
        adStarted = false;
        canMove = true;
        // Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
    }
 
    public void OnUnityAdsShowFailure(string _adUnitId, UnityAdsShowError error, string message)
    {
        Debug.Log($"Error showing Ad Unit {_adUnitId}: {error.ToString()} - {message}");
        adStarted = false;
        canMove = true;
        // Optionally execute code if the Ad Unit fails to show, such as loading another ad.
    }

    public void OnUnityAdsAdLoaded(string adId)
    {
        Debug.Log("ad loaded");
        Advertisement.Show(adUnitId);
        adStarted = true;
        Debug.Log("playing ad");
    }

    public void OnUnityAdsShowStart(string adId)
    {

    }

    public void OnUnityAdsShowClick(string adId)
    {

    }
}

In the editor, “ad loaded” is printed, and the errors “loadListener is null, you will not receive any callbacks” and “initializationListener is null, you will not receive any callbacks” are thrown. in the android build, “ad loaded” is NOT printed, and both errors again are thrown. In both cases no test ads are played.
Any help is appreciated!

The errors you’re encountering, specifically “loadListener is null, you will not receive any callbacks” and “initializationListener is null, you will not receive any callbacks,” indicate that the load listener and initialization listener are not properly set. This might be causing issues with receiving callbacks for ad loading and initialization

using UnityEngine;
using UnityEngine.Advertisements;

public class PlayerController : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
    // Other existing variables and methods...

    private void Start()
    {
        // Set the load and show listeners
        Advertisement.AddListener(this);
        Advertisement.Initialize(gameIdAndroid, testMode);
        adUnitId = adUnitIdAndroid;
    }

    // Other existing methods...

    public void OnUnityAdsReady(string placementId)
    {
        // Handle when an ad is ready to be shown for the specified placement ID
        Debug.Log("Ad is ready: " + placementId);
    }

    public void OnUnityAdsDidError(string message)
    {
        // Handle errors during ad loading or initialization
        Debug.LogError("Ad error: " + message);
    }

    public void OnUnityAdsDidStart(string placementId)
    {
        // Handle when an ad starts playing
        Debug.Log("Ad started: " + placementId);
    }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        // Handle when an ad finishes playing
        Debug.Log("Ad finished: " + placementId + ", result: " + showResult);

        // Resume movement if the ad was completed or skipped
        if (showResult == ShowResult.Finished || showResult == ShowResult.Skipped)
        {
            canMove = true;
            adStarted = false;
        }
    }
}

Ensure that you’ve added the correct using directive using UnityEngine.Advertisements; . The OnUnityAdsReady , OnUnityAdsDidError , OnUnityAdsDidStart , and OnUnityAdsDidFinish methods are now implemented to handle ad-related callbacks appropriately.