如何在 android 中接收推送通知时同步 firebase 数据库

How to sync firebase database while receiving push notification in android

我创建了一个运行良好的聊天应用程序,但在收到通知时我想同步 firebase 数据库,以便数据也处于离线模式。

我试过使用同步模式,但没用

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("somepath");
databaseReference.keepSynced(true);

但这在接收通知时不起作用。

简而言之,我想在后台服务(FirebaseMessagingService)中同步 firebase 数据库持久状态。

除非您的服务是 foreground service,并且显示通知,Android 将终止应用程序进程,并且同步将停止。因此,您必须将您的服务安排为前台服务,或者接受当用户未主动使用该应用程序时不会发生同步。