6
votes

I'm trying to implement Firebase remote config into unity project.

This is the only script that's running:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase;
using Firebase.RemoteConfig;
using System;
using System.Threading.Tasks;

public class FirebaseAlt : MonoBehaviour
{
    private  DependencyStatus dependencyStatus = DependencyStatus.UnavailableOther;

    void Awake()
    {
        FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
            dependencyStatus = task.Result;
            if (dependencyStatus == DependencyStatus.Available)
            {
                 InitializeFirebase();
            }
            else
            {
                 Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyStatus);
            }
         });
     }

     // Initialize remote config, and set the default values.
     void InitializeFirebase()
     {
          FetchData();
     }

     // Start a fetch request.
     public void FetchData()
     {
          Debug.Log("Fetching data...");
          // FetchAsync only fetches new data if the current data is older than the provided
          // timespan.  Otherwise it assumes the data is "recent enough", and does nothing.
          // By default the timespan is 12 hours, and for production apps, this is a good
          // number.  For this example though, it's set to a timespan of zero, so that
          // changes in the console will always show up immediately.
          Task fetchTask = FirebaseRemoteConfig.FetchAsync(TimeSpan.Zero);
          fetchTask.ContinueWith(FetchComplete);
      }

      void FetchComplete(Task fetchTask)
      {
           if (fetchTask.IsCanceled)
           {
               Debug.Log("Fetch canceled.");
           }
           else if (fetchTask.IsFaulted)
           {
               Debug.Log("Fetch encountered an error.");
           }
           else if (fetchTask.IsCompleted)
           {
               Debug.Log("Fetch completed successfully!");
           }

           var info = FirebaseRemoteConfig.Info;

           switch (info.LastFetchStatus)
           {
               case LastFetchStatus.Success:
                   FirebaseRemoteConfig.ActivateFetched();

                   Debug.Log(string.Format("Remote data loaded and ready (last fetch time {0}).", info.FetchTime));
                   string stop = FirebaseRemoteConfig.GetValue("stops").StringValue;
                   Debug.Log("Value: " + (string.IsNullOrEmpty(stop) ? "NA" : stop));

                   // Also tried this way, but then it doesn't enter the IF block
                   /*if (FirebaseRemoteConfig.ActivateFetched())
                   { 
                        Debug.Log(string.Format("Remote data loaded and ready (last fetch time {0}).", info.FetchTime));

                        string stop = FirebaseRemoteConfig.GetValue("stops").StringValue;
                        Debug.Log("Value: " + (string.IsNullOrEmpty(stop) ? "NA" : stop));
                   }*/
                   break;
               case LastFetchStatus.Failure:
                   switch (info.LastFetchFailureReason)
                   {
                       case FetchFailureReason.Error:
                             Debug.Log("Fetch failed for unknown reason");
                             break;
                       case FetchFailureReason.Throttled:
                             Debug.Log("Fetch throttled until " + info.ThrottledEndTime);
                             break;
                   }
                  break;
               case LastFetchStatus.Pending:
                  Debug.Log("Latest Fetch call still pending.");
                  break;
         }
     }
}

When I run this code I get all the right responses

"Fetching data...", "Fetch completed successfully!"

and

"Remote data loaded and ready (last fetch time 1/1/1970 12:00:00 AM)."

but the value is empty.

If I look what are the keys, I get a list of the default values (that I've put at the start of the test, but deleted that), but not any new keys I put in the firebase console (like the stops key I'm trying to show).

I tried almost any solution that I could find, but to no avail. I have the google-services.json (don't know if relevant for this), NDK version r13b. Changed FirebaseRemoteConfig.Settings.IsDeveloperMode to true. I Also tried to wait for a while (1 minute) after ActivateFetched() to see if it changes anything.

The API on ActivateFetched states that it returns:

true if there was a Fetched Config, and it was activated. false if no Fetched Config was found, or the Fetched Config was already activated.

How can I find the source for this problem? (why no fetched config was found. it exists in the projects console :/ )

1
Are you sure you clicked the "Publish" button in the Firebase console to make those values public?Todd Kerpelman
I haven't seen Publish button :(Newton

1 Answers

0
votes

It look like your Ip is mark as throttled because of having too much requests to remote config server. Just need to wait for another day and every thing should work or change computer. Here is a blog which lists most common mistake when working with remote config and how to prevent. Hope it will help. https://killertee.wordpress.com/2020/01/08/make-use-of-firebase-remote-config-in-your-unity-game/