0

I have zero knowledge about firebase. I have recently learned how to push data into the realtime database:

  1. I have created a Firebase project and included both my android app as well as website.

  2. After that I have included these script src into my html page:

     <script src="https://www.gstatic.com/firebasejs/9.10.0/firebase-app-compat.js"></script>
     <script src="https://www.gstatic.com/firebasejs/9.10.0/firebase-database-compat.js"></script>
    
  3. In my javascript, I am initializing my firebase with all the config details fetched from my project settings:

          const firebaseConfig = {
             apiKey: "AIzaSyBIJmvWbzdexxvyMGS-u5LFTOPlgWE6AO4",
             authDomain: "nfr-qtr-electric-billing.firebaseapp.com",
             projectId: "nfr-qtr-electric-billing",
             storageBucket: "nfr-qtr-electric-billing.appspot.com",
             messagingSenderId: "123052283905",
             databaseURL: "https://nfr-qtr-electric-billing-default-rtdb.firebaseio.com",
             appId: "1:123052283905:web:f0fc4ef2165122e112c800"
         };
         firebase.initializeApp(firebaseConfig);
    
  4. After that I am pushing some data into my realtime database which is in an array:

         for(var i=0; i<arr.length; i++){
             firebase.database().ref("emp_details/0/").push({
                   "Name":arr[i][0],
                   "Address":arr[i][1],
                   "Gender":arr[i][2]
             })
         }
    

Now what I want to do is, with each data being pushed into the database, a notification should be sent to the android phone. I have no idea, as to how to proceed. I have followed all the firebase documentation pages, but they are way too complicated to understand.

In my android app, I have created a Firebase messaging service class which should check for incoming notification messages and should show them:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
         //How do I show the notification here?
    }
}

Can anyone please help me out?

  • You probably don't want to share all the Firebase configuration (including ApiKey) publicly in your question. – CarlesCF Oct 20 '22 at 10:30
  • What can I say? I am desperate here. I have been stuck in this issue for a week now and it's driving me crazy –  Oct 20 '22 at 10:42
  • 1
    Check the answer I linked. Since you are using Realtime Database, the example use-case in the Cloud Functions documentation is especially relevant: https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens – Frank van Puffelen Oct 20 '22 at 10:43
  • 1
    @CarlesCF: the Firebase configuration OP shared here is different from what you might be used to. Have a look here to learn why that is: https://stackoverflow.com/questions/37482366/is-it-safe-to-expose-firebase-apikey-to-the-public – Frank van Puffelen Oct 20 '22 at 10:43
  • @Parker I consider your main concern is about architecture design. I would ask the questions where are the Database modifications coming from, who is triggering them. Then you can think about where to place your notification code (it may be using PubSub and CloudFunctions, for simplicity) – CarlesCF Oct 20 '22 at 10:50
  • Thanks @FrankvanPuffelen for pointing out! I read quickly and I thought it was some backend code that would allow anyone to send push notifications on his behalf. – CarlesCF Oct 20 '22 at 10:54
  • @CarlesCF Everyone does. I really wish we didn't have a key named `apiKey` in there. :) – Frank van Puffelen Oct 20 '22 at 11:15

1 Answers1

1

If you want to use the Firebase Cloud Messaging (FCM) service to send the notifications you need to "build message requests in a trusted server environment that supports the Firebase Admin SDK or the FCM server protocols" as explained in the FCM documentation.

This trusted server environment can be a server you manage or Cloud Functions for Firebase, the "serverless framework that lets you automatically run backend code in response to events triggered by Firebase features".

The documentation gives all the details to build message requests and send them.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121