1
votes

I have no idea why this happens, so I have several variables on the firebase remote config which is the Admob Id I want to fetch for loading the ads because I don't want to update from the script so I'm using firebase remote config to store the AdsId.

then the problem happens when the first launch of the App always no Ads request, but on the second launch the ads show up.

but if I'm not using firebase remote config or manually Add AdsId on the script, the first launch app the Ads show up

this preview :

  1. first time launch with fetch from firebase remote config : enter image description here
  2. this Second launch or re-open the app or add AdsId manually : enter image description here

here the script:

  1. Ads script :
 private void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(gameObject);
            return;
        }
        instance = this;
        DontDestroyOnLoad(this);

        bannerAdId = Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("Android_BannerAdsId").StringValue;
        InterstitialAdID = Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("Android_IntersitialAdsId").StringValue;
        rewarded_Ad_ID = Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("Android_VideoAdsId").StringValue;

        rewardedAd = RewardBasedVideoAd.Instance;
    }

    // Start is called before the first frame update
    void Start()
    {

        MobileAds.Initialize(GameID);

    }
  1. calling fetch on start :
 private void Start()
    {
        _FirebaseRemoteConfig.instance.InitializeFirebase();
        ....................................................
    }
 private void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
            instance = this;
        }
        InitializeFirebase();
    }

    public int countingUpdate;
    public int maxCount;

    Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;
    // Use this for initialization
    void Start()
    {

    }

    public void InitializeFirebase()
    {
        System.Collections.Generic.Dictionary<string, object> defaults =
            new System.Collections.Generic.Dictionary<string, object>();
        defaults.Add("config_test_string", "default local string");
        defaults.Add("config_test_int", 1);
        defaults.Add("config_test_float", 1.0);
        defaults.Add("config_test_bool", false);

        Firebase.RemoteConfig.FirebaseRemoteConfig.SetDefaults(defaults);
        Debug.Log("Remote config ready!");

        FetchFireBase();
    }
    public void FetchFireBase()
    {
        FetchDataAsync();
    }
  1. when add AdsId manually
 string GameID = "ca-app-pub-6738378604910121~6166276301";

    // Sample ads
    string bannerAdId = "ca-app-pub-3940256099942544/6300978111";
    string InterstitialAdID = "ca-app-pub-3940256099942544/1033173712";
    string rewarded_Ad_ID = "ca-app-pub-3940256099942544/5224354917";


    public BannerView bannerAd;
    public InterstitialAd interstitial;
    public RewardBasedVideoAd rewardedAd;

    public static _AdmobAds instance;

    private void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(gameObject);
            return;
        }
        instance = this;
        DontDestroyOnLoad(this);

        //bannerAdId = Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("Android_BannerAdsId").StringValue;
        //InterstitialAdID = Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("Android_IntersitialAdsId").StringValue;
        //rewarded_Ad_ID = Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("Android_VideoAdsId").StringValue;

        rewardedAd = RewardBasedVideoAd.Instance;
    }

    // Start is called before the first frame update
    void Start()
    {

        MobileAds.Initialize(GameID);

    }
1

1 Answers

2
votes

So first I'll just note that there's no code here awaiting for FirebaseApp.FetchDependenciesAsync() to update dependencyStatus. I'll assume that you have all of that architecture piped in already, but I'll call it out in case that isn't the case. Also you don't show the actual call to FetchAsync or where you call ActivateFetched. This is all necessary, but from the description it looks like you're already getting this far.

So, the way RemoteConfig works is that it will try to minimize bandwidth usage and any runtime costs. So you always have to explicitly fetch the data, then activate it asynchronously. If you do not set a default value, you will get null out of GetValue until either there's a fetched value on start (your second run) or you call ActivateFetched after the FetchAsync task completes. Note that it may not complete this run depending on if the data matches that on the server or the previous cache didn't expire -- so you cannot await on this before activating your ads.

Therefore, my main suggestion would be to go to where you set your defaults and add this:

defaults.Add("Android_BannerAdsId", bannerAdId);
defaults.Add("Android_IntersitialAdsId", InterstitialAdID);
defaults.Add("Android_VideoAdsId", rewarded_Ad_ID);

This way there's always some value on your first run.

If that doesn't work (ie: you can't get your ad ids where you set your remote config defaults), you can try using null propagation:

bannerAdId = Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("Android_BannerAdsId")?.StringValue ?? bannerAdId;
InterstitialAdID = Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("Android_IntersitialAdsId")?.StringValue ?? InterstitialAdID;
rewarded_Ad_ID = Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("Android_VideoAdsId")?.StringValue ?? rewarded_Ad_ID;

or check for the presence of your key in Keys before overriding your default values.