Firebase.RemoteConfig 在电脑上工作而不在 android

Firebase.RemoteConfig working on pc and don't on android

我想得到enter image description here这个参数。 我正在尝试使用这段代码来做到这一点。但它只能在电脑上运行。

这是我的所有代码,但我只需要从远程配置获取参数并初始化 firebase 分析。

我不知道问题出在哪里,因为关于统一的指南很少。 我正在尝试这样做已经 3 天了,请帮忙。

    using UnityEngine;
    using Firebase.RemoteConfig;
    using System;
    using System.Collections.Generic;
    using UnityEngine.SceneManagement;
    using System.Threading.Tasks;
    using Firebase.Crashlytics;
    using Firebase.Analytics;
    using Photon.Pun;
    using TMPro;
    using UnityEngine.UI;
    
    public class GameOpening : MonoBehaviour
    {
        [SerializeField] private Text _versionText;
        [SerializeField] private RemoteConfigSavings _remoteConfigSavings;
        private Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;
        
        private int _version = -1;
        private int _receivedVersion
        {
            get => _version;
            set
            {
                _version = value;          
                UpdateConfig();
            }
        }
        private void Start()
        {
            AwakeFireBase();
            Invoke("ToNextScene", 1);
        }
        private void ToNextScene()
        {
            if(_version!=-1) PhotonNetwork.LoadLevel(1);

            else Invoke("ToNextScene", 1);
        }
        private void AwakeFireBase()
        {
            if(Debug.isDebugBuild)
            {
                var configSettings = new ConfigSettings();
                configSettings.MinimumFetchInternalInMilliseconds = 0;
                FirebaseRemoteConfig.DefaultInstance.SetConfigSettingsAsync(configSettings);  
            }
    
            Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
                dependencyStatus = task.Result;
                if (dependencyStatus == Firebase.DependencyStatus.Available)
                {
                    //InitializeFirebase();
                    GetGameVersion();
                }
                else
                {
                    Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyStatus);
                }
            });
        }
    
        private void InitializeFirebase()
        {
            var defaults = new System.Collections.Generic.Dictionary<string, object>
            {
                {"someThing", "asdf"},
                {"some2", 12323}
            };

            FirebaseRemoteConfig.DefaultInstance.SetDefaultsAsync(defaults).ContinueWith(task=> { GetGameVersion(); });
            Debug.Log("Remote config ready!");
        }
        private void GetGameVersion()
        {
            var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
            remoteConfig.FetchAndActivateAsync().ContinueWith(task =>
            {
                IDictionary<string, ConfigValue> values = remoteConfig.AllValues;
                values.TryGetValue("VERSION", out ConfigValue objValue);
    
                int version = Convert.ToInt32(objValue.StringValue);
                _receivedVersion = version;
                _versionText.text = version.ToString();
            });
        }
        private void UpdateConfig()
        {
            _remoteConfigSavings.Version = _version;
            _remoteConfigSavings.SaveObject();
        }
    }

在我们开始之前,我们需要清理我们的代码。

除了与 FirebaseRemoteConfig 相关的部分外,我删除了所有内容。 另外,我删除了 CheckAndFixDependenciesAsync()SetDefaultsAsync() 部分。

在上面介绍的代码清理之后,剩下的就是GetGameVersion()方法。现在,让我们找出问题所在。

问题

您应该完全理解 FirebaseRemoteConfig API 页面中 FetchAndActivateAsync() 的描述。

FetchAndActivateAsync()

If the time elapsed since the last fetch from the Firebase Remote Config backend is more than the default minimum fetch interval, configs are fetched from the backend.

虽然未在此页面上列出,但默认情况下最小提取间隔为 12 小时

我们把那部分改成FetchAsync(TimeSpan.Zero),因为我们想在开发的时候实时检查这个值是否更新。

using UnityEngine;
using System;
using Firebase.Extensions;

public class GameOpening : MonoBehaviour
{
    private void Start()
    {
        GetGameVersion();
    }

    private static void GetGameVersion()
    {
        var remoteConfig = Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance;

        var fetchTask = remoteConfig.FetchAsync(TimeSpan.Zero)
            .ContinueWithOnMainThread(async task =>
            {
                await Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.ActivateAsync();
                
                var values = remoteConfig.AllValues;
                values.TryGetValue("VERSION", out var configValue);
                
                Debug.Log(configValue.StringValue);
            });
    }
}

看看你为什么使用 ActivateAsync()。我们使用 async await.

是有原因的

Note: This does not actually apply the data or make it accessible, it merely retrieves it and caches it. To accept and access the newly retrieved values, you must call ActivateAsync(). Note that this function is asynchronous, and will normally take an unspecified amount of time before completion.

所有问题都已解决。我还在 Android 上检查过它,它工作正常。 (基于三星 Galaxy 10 5G)(使用 LogCat)


希望你的问题得到解决:)