2

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?

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
HMT
  • 2,093
  • 1
  • 19
  • 51
  • you can create a REST API on your server and call it every time when a user does a new entry. – Pritish Joshi Mar 20 '18 at 06:37
  • Look into Cloud Functions for Firebase to write backend code that triggers in response to changes in your database. https://firebase.google.com/docs/functions/ – Doug Stevenson Mar 20 '18 at 06:41
  • In your onChildAdded call a web api using volley or retrofit whatever you want and send data with it to server app – Pritish Joshi Mar 20 '18 at 06:42
  • @DougStevenson I didn't exactly understand where should I be placing the code – HMT Mar 20 '18 at 06:45

1 Answers1

1

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.

Shababb Karim
  • 3,614
  • 1
  • 22
  • 35
  • I have mentioned a code above , according to this code , a toast must be displayed every time there is a new data base entry but the toast doesn't appear – HMT Mar 20 '18 at 09:17