firebase-unity-sdk icon indicating copy to clipboard operation
firebase-unity-sdk copied to clipboard

Custom config provided in code isn't recognized

Open cado1982 opened this issue 4 years ago • 2 comments

I'm attempting to provide Firebase config using FirebaseApp.Create but Analytics is logging errors that it can't find the google_app_id.

public class FirebaseAnalyticsManager : MonoBehaviour
{
    private void Awake()
    {
        InitializeFirebase();
    }

    private void InitializeFirebase()
    {
        Debug.Log("Initializing Firebase");

#if PRODUCTION
        SetProductionEnvironment();
#else
        SetDevelopmentEnvironment();
#endif

#if UNITY_IOS || UNITY_ANDROID
        FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
        {
            var dependencyStatus = task.Result;

            if (dependencyStatus == Firebase.DependencyStatus.Available)
            {
                Debug.Log("Firebase available");
                OnFirebaseAvailable();
            }
            else
            {
                Application.Quit();      
            }
        }); 
#endif
    }

    private void OnFirebaseAvailable()
    {
#if UNITY_IOS || UNITY_ANDROID
        FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
        SetupMessaging();
#endif
    }

    private static FirebaseApp SetDevelopmentEnvironment()
    {
        Debug.Log("Setting Development environment");
        AppOptions options = new AppOptions();
        options.ProjectId = "######";
        options.StorageBucket = "######.appspot.com";
        options.AppId = "######";
        options.ApiKey = "######";
        options.MessageSenderId = "######";
        options.DatabaseUrl = null;
        return FirebaseApp.Create(options);
    }

    private static FirebaseApp SetProductionEnvironment()
    {
        Debug.Log("Setting Production environment");
        AppOptions options = new AppOptions();
        options.ProjectId = "######";
        options.StorageBucket = "######.appspot.com";
        options.AppId = "######";
        options.ApiKey = "######";
        options.MessageSenderId = "######";
        options.DatabaseUrl = null;
        return FirebaseApp.Create(options);
    }
}

Running on an Android device provides the following logs from logcat

2021/09/20 20:08:29.429 24721 24867 Info Unity Initializing Firebase
2021/09/20 20:08:29.429 24721 24867 Info Unity Setting Production environment
2021/09/20 20:08:29.577 24721 24867 Info FirebaseApp Device unlocked: initializing all Firebase APIs for app [DEFAULT]
2021/09/20 20:08:29.658 24721 24867 Info FirebaseCrashlytics Initializing Firebase Crashlytics 18.0.1 for com.evenlycoolergames.biomesqa
2021/09/20 20:08:29.759 24721 24867 Info firebase Firebase Analytics API Initializing
2021/09/20 20:08:29.761 24721 24867 Info firebase analytics API Initialized
2021/09/20 20:08:29.792 24721 24867 Info firebase Firebase Cloud Messaging API Initialized
2021/09/20 20:08:29.844 24721 25288 Info Unity Firebase available
2021/09/20 20:08:30.016 24721 24867 Info Unity Firebase Analytics API Initializing
2021/09/20 20:08:30.017 24721 24867 Info Unity analytics API Initialized
2021/09/20 20:08:30.018 24721 24867 Info Unity Firebase Cloud Messaging API Initialized
2021/09/20 20:08:30.032 24721 25302 Info FA App measurement initialized, version: 43008
2021/09/20 20:08:30.032 24721 25302 Info FA To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2021/09/20 20:08:30.033 24721 25302 Info FA To enable faster debug mode event logging run:
2021/09/20 20:08:30.033 24721 25302 Info FA   adb shell setprop debug.firebase.analytics.app com.evenlycoolergames.biomesqa
2021/09/20 20:08:30.046 24721 25302 Error FA Missing google_app_id. Firebase Analytics disabled. See https://goo.gl/NAOOOI
2021/09/20 20:08:30.048 24721 25237 Warn FA Cannot log screen view event when the app is in the background.
2021/09/20 20:08:30.049 24721 25302 Error FA Uploading is not possible. App measurement disabled

As you can see my Initializing Firebase log message happens first. Then the SetProductionEnvironment method is called which from the docs should set the default instance. However all calls to Analytics are generating the Missing google_app_id. Firebase Analytics disabled log.

I don't have any google-services.json file, because I want to do the configuration in code. I'd expect FirebaseApp.Create to set the default instance but that doesn't seem to be the case. What am I missing?

cado1982 avatar Sep 20 '21 19:09 cado1982

Hi @cado1982

FirebaseApp.Create() need to be called AFTER FirebaseApp.CheckAndFixDependenciesAsync() is complete.

Could you move SetProductionEnvironment to OnFirebaseAvailable()? For instance

    private void OnFirebaseAvailable()
    {
#if PRODUCTION
        SetProductionEnvironment();
#else
        SetDevelopmentEnvironment();
#endif
#if UNITY_IOS || UNITY_ANDROID
        FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
        SetupMessaging();
#endif
    }

chkuang-g avatar Aug 27 '22 02:08 chkuang-g

Hey @cado1982. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Sep 02 '22 01:09 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@cado1982 if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar Sep 09 '22 01:09 google-oss-bot