I have an android app that allows users to write to the database. I have read from recent posts about how you can listen to data updates from particular childs in firebase database using firebase listener and service class. I have also heard however that it does not always work when the app is closed. I also heard for Firebase Cloud Messaging, but all the examples I have seen are in kotlin or Javascript What is the best and most efficient way to notify the user whenever a change occurs in the database keeping in mind that there may be several changes to the database during the course of the day, but each time a change occurs the old notification is replaced. Please note I only want to be notified when a change occurs in a child, let's say child "general_messages", how can I send a notification to users, let's say for instance a notification saying, "new change please login" whenever a change occurs in that child even if the data doesn't concern a particular user? I am quite new to firebase please give relevant assistance or direction
-
You don't need Java script code to notify the changes that have made in your local db changes. Instead of asking idea here you google it you can get many ideas related to your question – MathankumarK Nov 01 '19 at 12:25
-
I did Google, but all results and examples are in kotlin or Java script – Yayayaya Nov 01 '19 at 12:45
-
1You can check **[this](https://stackoverflow.com/questions/48298993/push-notifications-on-content-change/48299840)** out. – Alex Mamo Nov 01 '19 at 13:23
2 Answers
As I understand it your requirement is to push a notification to a device when data changes on the backend database EVEN if the application is not running on the users device.
As the device is not running the software you need to send the notification (using FCM) from the backend - when the phone receives the notification the application is started and you can process what you need to in the app.
Typically this is implemented using the following logic: 1. When the application is connected it saves what data it is interested in and a FCM token to represent this device somewhere in the firebase database. 2. You create a firebase cloud function that is run when certain areas of the database are changed. 3. This function has a look at what has changed; compares it to the data created in (1) and sends a notification to each device token that requires it.
The notification in step 3 can be a 'user' notification that comes up in the status bar and can be clicked ect to interact with the app OR it can be silent and just launch the app for processing in the background.

- 7,365
- 2
- 28
- 33
For complete detail instructions to setup firebase and other info, please see fire base doc provided for real time firebase data base
Step 1: Write to your database
Retrieve an instance of your database using getInstance()
and reference the location you want to write to.
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
Step 2: Read from your database
To make your app data update in realtime, you should add a ValueEventListener
to the reference you just created.
The onDataChange()
method in this class is triggered once when the listener is attached and again every time the data changes, including the children.
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
Note: In your Firebase Notification class which extends FirebaseMessagingService service you can publish a silent push and handle that silent push whatever you would like to do in your app.

- 6,073
- 4
- 29
- 47
-
I know how to write to the database and listen for changes in the database. I want to create a notification whenever a change occurs in the database while the app is closed or not running – Yayayaya Nov 01 '19 at 12:44
-
In your Firebase Notification class which extends FirebaseMessagingService service you can publish a silent push and handle that silent push whatever you would like to do in your app. – kj007 Nov 01 '19 at 12:47
-
May you please send a link that would show me how to create a firebase notification class and how to do all which you have mentioned – Yayayaya Nov 01 '19 at 12:51
-
https://firebase.google.com/docs/cloud-messaging/android/client, push will be send on device id by server so you need to store device id at server.I believe you will be using push notification service I am referring same. – kj007 Nov 01 '19 at 12:54