【问题标题】:How to Solve App Center Error? - "Default FirebaseApp is not initialized" For Xamarin.Android如何解决应用中心错误? -“默认 FirebaseApp 未初始化”对于 Xamarin.Android
【发布时间】:2020-04-25 14:40:31
【问题描述】:

我的 Xamarin.Android 应用程序在模拟器中运行良好,但最近当用户在实际的 Android 设备上运行它时刚刚开始崩溃。 应用中心显示此错误:

FirebaseInstanceId.get_Instance() Java.Lang.IllegalStateException:默认 FirebaseApp 未在此进程“NameOfApp”中初始化。请务必先调用 FirebaseApp.initializeApp(Context)。

一些用户建议在我的 MainActivty.cs 文件中编辑我的 OnCreate 方法,添加这个 sn-p 代码:

Firebase.FirebaseApp.InitializeApp(this);

我已经这样做了,但我不想推出另一个损坏的应用程序。请让我知道这是否是解决此错误的正确方法,或者是否有其他方法可以解决。

感谢您的宝贵时间!

 protected override void OnCreate(Bundle savedInstanceState)
        {
        Firebase.FirebaseApp.InitializeApp(this);

        base.OnCreate(savedInstanceState);
        if (!Parameter.CheckInternet())
        {
            Intent intent = new Intent(Application.Context, typeof(NoInternetActivity));
            StartActivity(intent);
            return;
        }
        Helper.TrackEvent(Parameter.CategoryLoad, "home", "Main");

        if (Intent.Extras != null)
        {
            // If there is a Network error show message
            if (Intent.Extras.ContainsKey("NetworkError"))
            {
                Toast.MakeText(this, "Network Error, please try again", ToastLength.Short).Show();
            }
            else
            {
                // If there is a link from a notification launch the appropriate activity
                string linkType = Intent.GetStringExtra("LinkType");
                string linkPage = Intent.GetStringExtra("LinkPage");
                if (linkPage.Equals("main"))
                {
                    // Link is a main page
                    LaunchMainActivity(linkType);
                }
                else
                {
                    if (linkType.Equals("video"))
                    {
                        // Link is a Youtube video
                        Helper.LaunchVideo(this, linkPage);
                    }
                    else
                    {
                        // Link is a single page
                        LaunchSingleActivity(linkType, linkPage);
                    }
                }
                // Used this at some point to debug the extras but it's not necessary
                foreach (var key in Intent.Extras.KeySet())
                {
                    if (key != null)
                    {
                        var value = Intent.Extras.GetString(key);
                        Helper.TrackEvent("DEBUG", "extras", $"Key: {key} Value: {value}");
                    }
                }
            }
        }

【问题讨论】:

    标签: android firebase xamarin mobile xamarin.android


    【解决方案1】:

    根据 AppCenter 的官方文档,https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm

    这是一个已知问题,这就是他们的建议

    “这是一个已知问题,您可以通过清理解决方案并重新构建项目(构建 > 清理解决方案、构建 > 重新构建解决方案)来解决。”

    我不建议添加您提到的代码。它不能保证您的应用程序可以正常工作。

    【讨论】:

    • 感谢您的帮助和回复,我会尽快尝试一下!
    • 不幸的是,我仍然通过 AppCenter 收到错误消息。请在下面查看我的堆栈跟踪,如果您碰巧有任何其他建议,请告诉我 - 感谢您的宝贵时间。
    • JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference 类型, Java.Interop.JniMethodInfo 方法, Java.Interop.JniArgumentValue* args) JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop .JniArgumentValue* 参数)FirebaseInstanceId.get_Instance () NewUpdateActivity.RunRegistration(System.Collections.Generic.List`1[T] 类别)AsyncMethodBuilderCore+c.b__7_1(System.Object 状态)
    • @Nantourakis 似乎也可能是另一个问题。你能分享那个函数调用的代码吗?
    • 谢谢@Saamer,我已经添加了我试图初始化 Firebase 的函数
    【解决方案2】:

    感谢大家的帮助,像下面这样手动配置 Firebase 解决了我的问题。

    var options = new FirebaseOptions.Builder()
    .SetApplicationId("<AppID>")
    .SetApiKey("<ApiKey>")
    .SetDatabaseUrl("<DBURl>")
    .SetStorageBucket("<StorageBucket>")
    .SetGcmSenderId("<SenderID>").Build();
    var fapp = FirebaseApp.InitializeApp(this, options);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-25
      • 2017-03-02
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      • 2022-10-05
      • 2019-07-05
      • 1970-01-01
      相关资源
      最近更新 更多