当 iOS 应用程序在后台时,您可以收听 Firestore 更新吗? [英] Can you listen to Firestore updates when iOS app is in the background?

查看:24
本文介绍了当 iOS 应用程序在后台时,您可以收听 Firestore 更新吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Firestore 非常陌生,正在尝试了解实时更新的工作原理.我现在正在使用这样的东西从 Firestore 获取更新:

I'm very new to Firestore and trying to understand how the real-time updates work. I'm using something like this now to get the updates from Firestore:

db
  .collection(Collections.session)
  .whereField("participants", arrayContains:userID)
  .addSnapshotListener { querySnapshot, error in

我注意到当应用程序在后台时,侦听器块没有被触发,而只有在它回到前台时才被触发.

I noticed that the listener block is not getting fired when the app is in the background, but only when it's brought back to foreground.

当应用程序也在后台运行时,有没有办法获取更新?也许以某种方式发送推送通知或其他什么?

Is there a way to get the update when the app is running in the background too? Maybe somehow send a push notification or something?

非常感谢任何形式的帮助.

Any kind of help is highly appreciated.

推荐答案

当应用程序也在后台运行时,有没有办法获取更新?

Is there a way to get the update when the app is running in the background too?

由于后台应用程序最终会被操作系统杀死,因此当用户没有主动使用应用程序时,您无法可靠地运行侦听器.

Since backgrounded apps are eventually killed by the OS, you don't have a way to run a listener reliably when the app is not actively being used by the user.

您是对的,(可靠地)通知您的应用后端发生某些变化的唯一方法是发送推送通知.

You are correct in that the only way to (reliably) notify your app of some change in your backend is to send a push notification.

一种非常常见的方法是使用 Cloud Functions 编写一个 Firestore 触发器,在创建、更新或删除感兴趣的文档时调用.您可以使用它来编写使用 Firebase Cloud MessagingFirebase Admin SDK 向您的应用发送通知一个负载,告诉它响应该更改.

A very common approach is to use Cloud Functions to write a Firestore trigger that gets invoked when a document of interest is created, updated, or deleted. You can use this to write backend code that uses Firebase Cloud Messaging and the Firebase Admin SDK to send a notification to your app with a payload that tells it to respond to that change.

这篇关于当 iOS 应用程序在后台时,您可以收听 Firestore 更新吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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