【问题标题】:Firebase realtime database does not work when the app is turned back on after the backgroud后台后重新开启应用时Firebase实时数据库不工作
【发布时间】:2017-03-05 09:23:17
【问题描述】:

我对 Firebase 有一个大问题。我很久以前在我的应用服务 Realtime Firebase 数据库中实现了。一切正常。然而,几个星期以来,我遇到了一些问题。当应用程序处于后台并停留一段时间时,Firebase 会脱机并且实时数据库服务无法正常工作。我在网上查了一下,发现这个东西有很多问题,但是我没有找到适合我的解决方案。我也收到以下错误:

W/PersistentConnection: pc_0 - Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your google-services.json file has the correct firebase_url and api_key. You can re-download google-services.json from https://console.firebase.google.com/.

当然,我尝试重新下载 json 文件,并检查了所有密钥。是数据库中的一个错误

这使我的应用程序无法使用。我尝试按照此https://itayhauptman.wordpress.com/2016/10/06/firebase-firebase-warning-provided-authentication-credentials-are-invalid-this-usually-indicates-your-firebaseapp-instance-was-not-initialized-correctly/ 指南创建服务帐户,

我尝试了这个Firebase for Android - W/PersistentConnection: pc_0 - Provided authentication credentials are invalid 问题中建议的solzuione,我尝试简化我的错误代码。所有这些行动都不能解决问题。我要做的最后一件事是在 Firebase 控制台中创建一个新应用程序。

在你这样做之前,有没有人有任何解决方案? 我真的很渴望这件事。 谢谢

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    Android 不会一直在后台运行您的 Firebase 应用。 Android 在睡眠或“打瞌睡”时可能会终止您的进程和服务。 如果需要,您可以使用 AlarmManager 唤醒您的应用并进行同步。

    如果您的同步是由某个事件触发的,请设置 firebase 云消息传递并在新消息到达时执行同步。

    在后台运行您的应用并实时连接到您的后端对于电池优化来说是一个非常糟糕的主意。

    编辑:

    如果您在不同的 android 进程中运行后台服务,则必须通过在 Application 类或您的服务类中调用 FirebaseApp.initializeApp(context); 再次初始化 Firebase;在任何用法之前。

    【讨论】:

    • 不,我不希望 Firebase 服务在后台继续运行。我的案例研究是在背景之后。重新激活应用程序后,该服务就像 Firebase 处于离线状态,如果您不重新登录,就无法重新激活它。 Quidni 我应该在每次重新启动应用程序时登录。我希望我解释了
    • FirebaseAuth 应该保持会话和身份验证。您应该不需要再次登录。
    • @hulon 我相信您在不同的进程中运行您的服务。在这种情况下,您必须通过调用 FirebaseApp.initializeApp(context); 再次初始化 Firebase 单例
    猜你喜欢
    • 1970-01-01
    • 2018-09-14
    • 2021-07-13
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 2019-10-27
    • 2021-01-29
    • 2014-02-23
    相关资源
    最近更新 更多