How to get notifications of newly added data in firestore android studio?

chat app using firebase: get notification when new message received - android
firebase cloud messaging notification not received by device android
how to handle notification when app in background in firebase android
firebase push notification android example 2019
missing default notification channel metadata in androidmanifest. default value will be used.
android push notifications
firestore detach listener
how to get notification data in android

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.

                .addSnapshotListener(new EventListener<QuerySnapshot>() {
                    public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e) {
                        if (e != null) {
                            Log.e(LOG_DB, e.toString());
                        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);

If you just want to send notifications, you can use Firebase Cloud Messages which may provide the functionality you are trying to implement yourself.

If you want to send a Notification after data is changed in your Firestore you can use FireStore Triggers ( and send a Notification via a firebase function call (Send push notifications using Cloud Functions for Firebase)

Sending push notification with Firebase, Firestore & Cloud Functions , After the launch of Firestore, a lot of people asked me about how to integrate push notifications for Firestore changes and new data additions, and onCreate(​event => { var newValue =; var message = "New Todo Added And if you need to send push notifications to specific users, get the  When using Firebase, an Android device can be notified when data changes. In this video, we describe how to be notified of that change. This video is part of a playlist on Android development

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.

Get realtime updates with Cloud Firestore, Compare Cloud Firestore and Realtime Database · Data model · Data types If you want foregrounded apps to receive notification messages or data To use FirebaseMessagingService , you need to add the following in your app manifest: Once your app is removed from background restriction, new messages to the  If you navigate to the Firebase console, you should see the newly added data: Congratulations, you just wrote data to Firestore! In the next step we'll learn how to display this data in the app. In this step we will learn how to retrieve data from Firestore and display it in our app. The first step to reading data from Firestore is to create a

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.

Receive messages in an Android app, Most of the applications you will have to develop will be required to notify Cloud Firestore is a flexible, scalable database for mobile, web, and Using FCM, you can notify a client app that new email or other data is available to sync. Create a new project in android studio and define firebase services  0 How to get notifications of newly added data in firestore android studio? Oct 9 '19. 0 Each then() should return a value or throw Notifications [duplicate] Oct 10 '19.

Android Push Notification with Firebase Cloud Messaging, Google Data Studio Contact Sales Get started for free However, you may want to add single-field exemptions in the following cases: Exceeding these recommendations can result in increased notification latency. To ensure data consistency, Firestore needs to prime each new snapshot listener from its source data  In this part we will do the followings: Create Project. Design UI. Connect our app with Firebase. Enable Cloud Firestore. Add/Upload data to Cloud Firestore.

Send Notification on Data change in firebase Database || Firebase , There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries:

Get notification of changes in Firebase on Android, Add a new document to a collection. In this case, Cloud Firestore automatically generates the document identifier. Create an empty document with an automatically generated identifier, and assign data to it later. This guide explains how to use the set, add, or update individual documents in Cloud Firestore.