Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send notification automatically from Firebase

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.

like image 250
Abanoub Samaan Avatar asked May 31 '16 09:05

Abanoub Samaan


People also ask

Can Firebase send push notification?

Firebase Cloud Messaging (FCM) provides a reliable and battery-efficient connection between your server and devices that allows you to deliver and receive messages and notifications on iOS, Android, and the web at no cost.

How do I automate Firebase Cloud Messaging?

Go to the app build gradle file, in the dependencies section, add the Firebase Cloud Messaging dependency and Sync the project. Once the process is finished create a new class call PushNotification Service and have it extend the FirebaseMessagingService class.

How do I trigger Firebase notification?

For sending FCM notification payload you can use Firebase Cloud Messaging Tool in firebase console. And click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token.


2 Answers

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

like image 92
Tom Saju Avatar answered Oct 18 '22 19:10

Tom Saju


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);
like image 35
Talha Mir Avatar answered Oct 18 '22 19:10

Talha Mir