【问题标题】:How to get notifications of newly added data in firestore android studio?如何在firestore android studio中获取新添加数据的通知?
【发布时间】:2020-02-06 14:02:59
【问题描述】:

我正在我的应用程序上做一个通知,我正在使用 Firestore。我的问题是我想在snapshotlistener 检测到数据库中新添加的数据时向用户发送通知但是当我打开应用程序时,即使我没有添加新数据,它也会立即显示现有通知。我需要一些条件,我只能获取新添加的数据,或者如果我的数据库数据中缺少某些东西,需要克服这个问题。下面是我的数据库结构。

db.collection("Buyers")
                .document(userID)
                .collection("Notifications")
                .addSnapshotListener(new EventListener<QuerySnapshot>() {
                    @Override
                    public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e) {
                        if (e != null) {
                            Log.e(LOG_DB, e.toString());
                            return;
                        }
                        for (DocumentChange dc : snapshots.getDocumentChanges()) {

                            if (dc.getType() == DocumentChange.Type.ADDED) {
                                String title = dc.getDocument().getData().get("notificationTitle").toString();
                                String body = dc.getDocument().getData().get("notificationBody").toString();

                                //Method to set Notifications
                                getNotification(title, body);
                            }
                        }
                    }
                });

【问题讨论】:

    标签: java android notifications google-cloud-firestore snapshot


    【解决方案1】:

    如果您只想发送通知,可以使用 Firebase Cloud Messages,它可能会提供您尝试自己实现的功能。 https://firebase.google.com/docs/cloud-messaging

    如果您想在 Firestore 中的数据发生更改后发送通知,您可以使用 FireStore 触发器 (https://firebase.google.com/docs/functions/firestore-events) 并通过 firebase 函数调用 (Send push notifications using Cloud Functions for Firebase) 发送通知

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,我就是这样解决的:

      1. 获取您当前添加的项目的计数并保存在共享首选项中

      2. 打开应用后,获取当前项目数量并与共享偏好中保存的数量进行比较

      3. 设置一个条件,如果当前项目数超过共享首选项中保存的数量,则调用通知。

      【讨论】:

      • 假设我们在服务器上有 10 个本地通知和 12 个通知,其中 2 个将作为新项目添加。但是随后 3 个之前的发件人撤销了他们的操作(作为示例取消),所以现在我们在服务器上有 9 个通知,其中 2 个仍然是新通知,并且它们不会被检测到,因为保存的数量将大于当前数量。
      【解决方案3】:

      我能够得到我想要的,但我不确定这是否是正确的方法。

      Calendar calendar = Calendar.getInstance();
          calendar.add(Calendar.MINUTE, -5);
          Date before = calendar.getTime();
      
          Calendar calendar1 = Calendar.getInstance();
          Date until = calendar1.getTime();
      
          for (DocumentChange dc : snapshots.getDocumentChanges()) {
              Date date = (Date) dc.getDocument().getData().get("notificationDate");
              if (dc.getType() == DocumentChange.Type.ADDED) {
                  if (!before.after(date) && !until.before(date)){
                      Log.d("life", "Data: "+date);
                      String title = dc.getDocument().getData().get("notificationTitle").toString();
                      String body = dc.getDocument().getData().get("notificationBody").toString();
                      getNotification(title, body);
                  }
              }
          }
      

      我在这里所做的是检索当前时间和当前时间减去 5 分钟。(您可以选择所需的延迟时间)然后设定一个条件,它必须只显示延迟 5 分钟内的通知。

      注意: 我知道这不是正确的做法,但这得到了我想要的结果。如果您不想要我的回答,请告诉我并发布您自己的答案,以便我确认您的回答。

      【讨论】:

        猜你喜欢
        • 2020-03-23
        • 1970-01-01
        • 2021-05-13
        • 2021-10-18
        • 2021-04-11
        • 2020-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多