【发布时间】: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