问题描述
我是Firebase实时数据库的新手,有一个基本问题,我似乎找不到答案.
I'm new to firebase realtime database, and have a basic question I cannot seem to find the answer to.
我需要我的Android应用程序来跟踪数据库中的更改,因此我了解我需要通过onDataChange方法使用addValueEventListener.但是,即使应用程序被破坏,也会调用onDataChange方法吗?即使该应用程序不是在后台运行,我也需要能够访问数据库中信息的更改(例如,用户强制退出了该应用程序).这是因为当值达到某个点时,我想显示一个弹出通知,因此即使应用程序未运行,我也需要能够读取这些值.
I need my Android app to keep track of changes in the database, so I understand I need to use addValueEventListener, with the onDataChange method. However will the method onDataChange, be called even if the app is destroyed? I need to be able to access the changes of the information in the database even if the app isn't running in the background, (for example the user force quits the app). This is because when the values reach a certain point, I want to show a pop up notification, so I need to able to read the values even when the app isn't running.
如果即使在应用程序处于后台时也调用了onDataChange,由于手机始终在监听更改,因此这会消耗电池电量.
If the onDataChange is called even when the app is in the background, will this drain battery use since the phone is always listening for changes.
很抱歉出现了基本问题,但我找不到该信息.
Sorry for the basic question, but I couldn't find the info.
谢谢!
推荐答案
事件侦听器仅在运行它们的上下文处于活动状态时才处于活动状态.对于侦听器,您附加到活动中,这意味着它们在应用程序运行时处于活动状态.即使这样,如果用户没有积极使用该应用,Android可能会杀死监听器(或更准确地说是:监听器用于与服务器通信的套接字).
Event listeners are only active while the context they run in is active. For listeners you attach in an activity, that means they're active while the app is running. Even then Android might kill the listeners (or more accurately: the socket they use for communicating with the server) if the user isn't actively using the app.
如果希望侦听器保持更长时间的活动,则可以考虑在后台服务中管理侦听器.但是即使在那儿,Android可能也会关闭监听器以延长电池寿命.这是始终牢记的一件事:如果您的用例干扰了用户的喜好(并且大多数用户可能强烈希望电池寿命长于任何特定的应用程序功能),那么长期来看就不太可能继续工作.
If you want the listener to stay active longer, you can indeed consider managing the listeners in a background service. But even there, Android might close the listener to preserve battery life. That is the one thing to always keep in mind: if your use-case interferes with the user's preference (and most users are likely strongly prefer longer battery life over any specific app feature), it's unlikely to continue working in the long run.
一种更好的方法是将侦听器与Firebase Cloud Messaging结合使用以发送消息.当用户不积极使用应用程序时,FCM消息更有可能(尽管仍不能保证)传递,并且您可以使用它们来运行应用程序的某些代码.您将需要为此使用FCM的数据消息,这是大多数应用程序提供后台更新的方式.
A better approach is to combine listeners with Firebase Cloud Messaging for sending messages. FCM messages are more likely (though still not guaranteed) to be delivered when the user is not actively using the app, and you can use them to run some code of your app when they arrive. You'll want to use FCM's data messages for this, which is how most app deliver background updates.
您还可以使用FCM数据消息来唤醒您自己的代码,然后使该代码附加一个侦听器并获取其更新.这种行为被称为发送挠痒痒,因为所有数据消息都在唤醒应用程序代码.
You can also use an FCM data message to just wake up your own code, then have that code attach a listener, and get its updates. This type of behavior is known as sending a tickle, since all the data message does is waking the application code up.
这篇关于Android:App销毁时更新Firebase实时数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!