在 Android 和 FCM 的后台处理 keepSynced() [英] Handling keepSynced() while on background on Android and with FCM

查看:21
本文介绍了在 Android 和 FCM 的后台处理 keepSynced()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑对 Firebase 实时数据库中的一些数据使用 keepSynced().我知道它会自动同步这些路径.但这与 Android 生命周期有何关系?如果用户离开所有活动(并且所有普通侦听器都断开连接),它会停止同步吗?我不希望该应用成为数据或电池消耗者.

I'm considering the use of keepSynced() for some data from Firebase Realtime Database. I understand that it will automatically sync those paths. But how does that relate to Android lifecycle? If the user leaves all activities (and all normal listeners disconnect), will it stop syncing? I don't want the app to become data or battery hog.

另一方面,我想在 FCM 通知到达时更新缓存数据.我可以启动一些连接到 Firebase 的服务.我想同步 keepSynced() 中的所有路径,并在同步时停止它.我不确定如何实现这一目标.为其中一个路径创建一个侦听器并使服务保持运行一段时间?服务结束后,会不会停止同步?

On the other hand, I would like to update cached data when FCM notification arrives. I can launch some service which will connect to Firebase. I would like to sync all paths which are in keepSynced() and stop it when it's synced. I'm not sure how to achieve that. Create a listener to one of the paths and keep the service running for some time? After the service is finished, will it stop syncing?

推荐答案

firebaser here

好问题!

当没有活动活动时,操作系统可能会随时关闭与 Firebase 数据库的连接.我们的 SDK 不会尝试阻止这种情况,但是当应用再次激活时重新连接.

When there is no active activity, the operating system may close the connection to the Firebase database at any time. Our SDKs don't try to prevent that, but will reconnect when the app becomes active again.

您在第二段中描述的是我们所谓的推送同步",您可以在其中发送推送通知(通常是无提示的 FCM 数据消息)以触发数据同步.

What you're describing in your second paragraph is what we call "push to sync", where you send a push notification (typically a silent FCM data message) to trigger synchronizing of the data.

我们在去年的 I/O 应用程序中做了类似的事情,虽然它比我们想要的要复杂一些,但效果很好.在这种情况下,我们显式管理连接,调用 goOnline()goOffline()(5 分钟后 iirc).主要同步代码可以在 IOSched github 存储库.

We did something like that in last year's I/O app and, while it was a bit more complex than we wanted it to be, it worked great. We explicitly managed the connection in that case, calling goOnline() and goOffline() (after 5 minutes iirc). The main sync code can be found in the IOSched github repo.

这篇关于在 Android 和 FCM 的后台处理 keepSynced()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆