如何在firestore android studio中获取新添加数据的通知?

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

https://img1.sycdn.imooc.com/6527b8b4000158c913640390.jpg

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);

                            }

                        }

                    }

                });


繁花如伊
浏览 114回答 3
3回答

鸿蒙传说

如果您只想发送通知,可以使用 Firebase Cloud Messages,它可以提供您尝试自己实现的功能。&nbsp;https://firebase.google.com/docs/cloud-messaging如果您想在 Firestore 中的数据更改后发送通知,您可以使用 FireStore 触发器 (&nbsp;https://firebase.google.com/docs/functions/firestore-events&nbsp;) 并通过 firebase 函数调用发送通知 (发送推送使用 Cloud Functions for Firebase 的通知)

呼唤远方

我有一个类似的问题,这就是我解决它的方法:获取当前添加的项目数并保存在共享首选项中打开应用程序后,获取当前的项目数量,并与共享首选项中保存的数量进行比较设置一个条件,如果当前项目数大于共享首选项中保存的数量,则调用通知。

神不在的星期二

我能够得到我想要的东西,但我不确定这是否是正确的方法。Calendar calendar = Calendar.getInstance();&nbsp; &nbsp; calendar.add(Calendar.MINUTE, -5);&nbsp; &nbsp; Date before = calendar.getTime();&nbsp; &nbsp; Calendar calendar1 = Calendar.getInstance();&nbsp; &nbsp; Date until = calendar1.getTime();&nbsp; &nbsp; for (DocumentChange dc : snapshots.getDocumentChanges()) {&nbsp; &nbsp; &nbsp; &nbsp; Date date = (Date) dc.getDocument().getData().get("notificationDate");&nbsp; &nbsp; &nbsp; &nbsp; if (dc.getType() == DocumentChange.Type.ADDED) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!before.after(date) && !until.before(date)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("life", "Data: "+date);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String title = dc.getDocument().getData().get("notificationTitle").toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String body = dc.getDocument().getData().get("notificationBody").toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getNotification(title, body);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }我在这里所做的是检索当前时间和当前时间减去 5 分钟。(您可以选择所需的延迟分钟数),然后设定一个条件,必须仅显示延迟 5 分钟内的通知。注意: 我知道这不是正确的做法,但这得到了我想要的结果。如果您不想要我的答案,请告诉我并发布您自己的答案,以便我确认您的答案。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java