How does firebase work for sending notifications?

Asked

Viewed 604 times

2

This question is not about codes but understanding the functioning of firebase.

I want to build a system that notifies the user in an Android app through a PHP web service. For what I researched, the easiest way is using firebase(and worked perfectly with me), but wanted to set up an independent web service.

My question is :

How does firebase notify the phone? From what I saw, we can make the mobile check if there is any notification every X minutes, but this would not consume enough data traffic?

How does firebase notify the user instantly? I wanted to understand this logic, to prevent the user to keep checking that there are new notifications every X minutes.

2 answers

2


It works exactly as you’re thinking: a service on your device maintains a connection to Google’s servers.

The key difference is that this service serves multiple servers/clients, all those who have overwritten the service, and not just your server/client.

Having a single service, serving multiple servers and providing/distributing messages to multiple clients, allows traffic to be used more efficiently instead of each client/server having its own service.

                ------------        ------------        ------------
                |Servidor A|        |Servidor B|        |Servidor C|
                ------------        ------------        ------------
                     |                    |                   |
                     |                    |                   |
                     ---------------------|--------------------
                                          |
                                       -------
                                       | FCM |
                                       ------- 
                                          |
                                          |
                       -------------------|--------------------
                       |                  |                   |
                ---------------     ---------------     ---------------
                |Dispositivo 1|     |Dispositivo 2|     |Dispositivo 3|
                ---------------     ---------------     ---------------
                   |        |          |        |              |       
                -------  -------    -------  -------        -------
                |App 1|  |App 2|    |App 1|  |App 2|        |App 2|
                -------  -------    -------  -------        -------

The Firebase Cloud Messaging acts as an intermediary between your server and devices.
He is responsible for receiving messages, managing them and forwarding them to their devices.

  • Thank you, Reply clarified the operation. Now could you let me know if it’s bad practice to make a service call yourself every x minutes to check notification instead of using firebase ?

  • 1

    Not exactly. The only problem is the one you’ve already identified: consuming more data traffic, and consuming resources (memory and cpu) from the device.

0

You will, on the server side (in your case in PHP), after some event (for example, at the end of your server save method), call the firebase library to send a push message to a user through a unique identifier (made available by firebase library on device). Example in nodejs: https://firebase.google.com/docs/cloud-messaging/admin/send-messages?hl=pt-br.

On the device (IOS, ANDROID...), you will implement an onmessage method (made available by the firebase library on the device), which will be automatically triggered when a push comes to the unique identifier of this device. Example for a Javascrip client: https://firebase.google.com/docs/cloud-messaging/js/receive?hl=pt-br.

  • Hello, thanks for the help, but as mentioned in the topic,I’ve been able to use firebase to send notification,the question is,as firebase notifies the phone without cell phone update x in x minutes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.