HMT
HMT

Reputation: 2261

How to Send Notifications to Server App when a New Entry is Added to the Database

I am working with two apps . one is the client app and the other one is the server app . As soon as the client places an order. A new entry is created in the database I want to send notification to the server app as soon as the customer places an order.

database = FirebaseDatabase.getInstance();
    myRef = database.getReference("requests");
    myRef.addChildEventListener(new ChildEventListener() {
        @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Toast.makeText(MainActivity.this,"new entry",Toast.LENGTH_SHORT).show();
            Log.d(TAG,"I know whats cooking " );
        }

this code will be used in background activity to detect a change in the database. What will be the best practice? What if we use the realtime firebase triggers the onCreate() method which is triggered whenever a new entry is added to the database . I am unable to understand where should I place this trigger code that I found in this link I found a similar question but couldn't find the answer. How to show notification from background service?

Upvotes: 2

Views: 481

Answers (1)

Shababb Karim
Shababb Karim

Reputation: 3713

You can use Cloud Functions to do exactly that. What you should do is register a onCreate function to your database reference. This function gets triggered only when a new child is created to your database reference. Then use FCM push notification API to send your notification.

Upvotes: 1

Related Questions