Send notification automatically from Firebase

12,642

Solution 1

Since the 9th of march 2017 Firebase introduced “Firebase Functions”.It helps to trigger some events on specific dataset changes.These events could be then available in the Firebase Notifications Console to trigger the push Notification.

Take a look at https://firebase.googleblog.com/2017/03/introducing-cloud-functions-for-firebase.html

Solution 2

Firebase provides push notification but not on database table changes. What you can do, you can create listeners in a background service and fire a notification from those listeners.

For instance, for listening to changes in 'User' node.

 FirebaseDatabase myFirebaseRef = FirebaseDatabase.getInstance();
 DatabaseReference myRef = myFirebaseRef.getReference("User");

 ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

                 //put your notification code here
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.i("FirebaseError", databaseError.getMessage());
        }
    };

 myRef.addValueEventListener(valueEventListener);
Share:
12,642
Abanoub Samaan
Author by

Abanoub Samaan

I'm working at Softxpert Inc. as a mobile developer since June 2015. Projects: 1- Docufy - Free Scan to PDF Use “Smart Document Scanner” as an ultimate android scanner app to scan, enhance, fax, convert into PDF file, OCR, convert jpeg to pdf file, add annotations, sync and file your prescriptions, invoices, contracts, bank statements, whiteboards and much more and gain accessibility to them from anywhere and at anytime. Featured Application by GOOGLE. Recommended by CES Mobile Apps Showdown - 'Android Mobile Scanner' SAP’s Pick ‘Top 5 Apps’ - Android Mobile Scanner Google play link : https://play.google.com/store/apps/details?id=com.softxpert.sds 2- Fax plus Simply grab your smart phone, scan your document with your phone camera or any document scanning app and then tap a single button in our fax app to send it anywhere in the world in just a few seconds. Google play link : https://play.google.com/store/apps/details?id=com.softxpert.android.fax 3- Walkthrough library A simple library to build a simple walkthrough activiy. Github link : https://github.com/AbanoubAsaad/Walkthrough Android arsenal link : https://android-arsenal.com/details/1/3582

Updated on July 26, 2022

Comments

  • Abanoub Samaan
    Abanoub Samaan over 1 year

    I am developing an android app using Firebase. Can Firebase send push notifications automatically when a record inserted to a table or I must implement my own server.

  • Arthur Thompson
    Arthur Thompson almost 8 years
    While this approach will work, long lived listeners can be taxing on the battery of a device. When your app is in the foreground listening is fine but once your app is in the background you should close your listeners.
  • androidXP
    androidXP almost 7 years
    basically.this would work only for testing but not in real scenario cuz as @ArthurThompson said it will effect battery and network. Best to install Firebase SDK on your server where Your functions will listen change in your database via firebase functions and will make post request to FCM.
  • Famic Tech
    Famic Tech over 6 years
    Agreed with the two above comments, Firebase Functions is the only way to go. With the new feature added it would be foolish to implement any other workaround.