Firebase实时数据库事件未在APK中触发,但在编辑器中

时间:2017-03-02 14:01:51

标签: android unity3d firebase firebase-realtime-database

我构建了一个需要侦听数据库对象更新的Unity游戏。当我在Unity编辑器中运行游戏时 - 触发实时数据库更改事件并且游戏会做出相应的反应。但是,当我发布到我的Android设备时,这些事件似乎没有注册。

当我检查Unity日志时,我看到的错误是:

InitializationException:Firebase模块无法初始化:邀请(缺少依赖项),消息传递(缺少依赖项),remote_config(缺少依赖项)

虽然我显然已经导入了这些包。

有什么想法吗?

firebaseReference.GetValueAsync().ContinueWith(task => {
        if (task.IsFaulted) {
            // Handle the error...
            print ("failed watchers");
        }
        else if (task.IsCompleted) {
            DataSnapshot snapshot = task.Result;
            // Do something with snapshot...
            print ("set watchers");
        }
    });

    firebaseReference.ChildChanged += HandleChildChanged;

1 个答案:

答案 0 :(得分:0)

此错误与Google服务有关;你有两个解决方案(我只发布与消息相关的信息,这是我用过的唯一一个):
 1)使用Firebase插件检查依赖项,并向用户显示一条消息(插件为您执行此操作)。
 2)尝试注册Firebase消息服务,捕获任何错误并静默失败(如果是这种情况);

以下是解决方案1的代码:
PushNotificationManager.cs

using UnityEngine;
using Firebase;

public class PushNotificationManager : MonoBehaviour {
    DependencyStatus dependencyStatus = DependencyStatus.UnavailableOther;

    public void Start()
    {
        dependencyStatus = FirebaseApp.CheckDependencies();
        if (dependencyStatus != DependencyStatus.Available)
        {
            FirebaseApp.FixDependenciesAsync().ContinueWith(task =>
            {
                dependencyStatus = FirebaseApp.CheckDependencies();
                if (dependencyStatus == DependencyStatus.Available)
                {
                    InitializeFirebase();
                }
                else
                {
                    Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyStatus);
                }
            });
        }
        else
        {
            InitializeFirebase();
        }    
    }


    protected void InitializeFirebase()
    {
        Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
        Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
    }
    public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
    {
        Debug.Log(token.Token);
    }

    public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
    {
        //Debug.Log("Received a new message from: " + e.Message.From + " / " + e.Message.Data.ToString());
    }
}

以下是解决方案2的代码:
PushNotificationManager.cs

    using UnityEngine;
    using System;

    public class PushNotificationManager : MonoBehaviour {
        public void Start()
        {
            InitializeFirebase();
        }


        protected void InitializeFirebase()
        {
           try
           {
              Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
              Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
           }
           catch(Exception e)
           {
               Debug.LogError("Erro no Firebase: "+e);


           }
        }
 //   (...)
}

注意: 据我所知,到目前为止还没有解决方案,所以这只是为了防止你的应用程序崩溃。 更多信息:Bugs reported [Firebase-Unity]