首页 > 解决方案 > 有什么方法可以使用 Android 应用程序捕获云 Firestore 上的任何更新,然后在 C# 应用程序中触发监听事件?

问题描述

我的团队想要将 android UI 与 Cloud Firestore 数据库一起使用。我们的一些团队是 c# 开发人员。我们希望创建一个事件,以便在云 Firestore 数据库发生更改时始终触发。有什么办法吗?

我想在数据库更改时触发此方法。

  // [START fs_listen_document] sample event trigger c# using Google.Cloud.Firestore; 
                DocumentReference docRef = db.Collection("test").Document("subtest");
                FirestoreChangeListener listener = docRef.Listen(snapshot =>
                {
                    Console.WriteLine("Callback received document snapshot.");
                    Console.WriteLine("Document exists? {0}", snapshot.Exists);
                    if (snapshot.Exists)
                    {
                        Console.WriteLine("Document data for {0} document:", snapshot.Id);
                        Dictionary<string, object> city = snapshot.ToDictionary();
                        foreach (KeyValuePair<string, object> pair in city)
                        {
                            Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
                        }
                    }
                });
                // [END fs_listen_document]

标签: c#asp.netfirebaseweb-servicesgoogle-cloud-firestore

解决方案


当 Firestore 发生任何事情时,客户端应用程序无法简单地触发一些代码。有数据库查询侦听器,但它们不会按您期望的方式工作。它们旨在随着时间的推移监视特定查询结果的结果。

提供的从 Firestore 接收事件的方法是通过Cloud Functions。您可以编写将在文档更改与您提供的模式匹配时触发的后端代码。然后,您可以决定如何处理。如果您认为客户端应用程序对更新感兴趣,也许您想向它发送推送通知。


推荐阅读