3

I am doing a notification on my app and I am using firestore. My problem is I want to send a notification to the user when the snapshotlistener detect a newly added data to the database But when I open the app it will show the existing notification right away even though i did not added a new data. I need some conditions where I can only get the newly added data or if there's something lacking in my database data that will need in order to overcome this issue. Below is my databse structure.enter image description here

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);
                            }
                        }
                    }
                });
Mr. Baks
  • 297
  • 2
  • 6
  • 20

3 Answers3

6

If you just want to send notifications, you can use Firebase Cloud Messages which may provide the functionality you are trying to implement yourself. https://firebase.google.com/docs/cloud-messaging

If you want to send a Notification after data is changed in your Firestore you can use FireStore Triggers (https://firebase.google.com/docs/functions/firestore-events) and send a Notification via a firebase function call (Send push notifications using Cloud Functions for Firebase)

Traendy
  • 1,423
  • 15
  • 17
2

I had a similar issue and this is how I solved it:

  1. Get a count of your current items added and save in Shared Preferences

  2. Upon opening the app get the current count of items and compare with the saved number in shared preferences

  3. Set a condition where if the current count of item is more than the saved number in shared preferences, the notification is called.

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • Let's say we have 10 local notifications and 12 notifications on the server where 2 of them are going to be added as new items. But then 3 previous senders revoked their action (unfollowed as an example), so now we have 9 notifications on the server where 2 of them are still new notifications, and they won't be detected because saved number will be greater than current count. – netsplatter Aug 25 '21 at 21:57
0

I am able to get what I want but I am not sure if this is the right way to do this.

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

What i have done here was I retrieve the current and the current time minus 5 mins.(You can choose how many delayed the mins you want) then made a condition where it must only show the notifications within the 5mins delayed date.

Note: I know this was not the proper practice but this gets the result that I want. If you didn't want my answer please let me know and post your own answer so I can acknowledge your answer.

Mr. Baks
  • 297
  • 2
  • 6
  • 20