3

I need to retrieve data from firebase database while app is in background or closed. For this, I have been thinking of using database reference in a service.

Instead of this, people recommend using Firebase Cloud Messaging. But what exactly is that?

How does is interact with the database? How do I use it for listening and retrieving data? There are very little explanation on these and firebase docs are too broad. Or maybe I just cant find it

AL.
  • 36,815
  • 10
  • 142
  • 281
  • The documentation is broad because the cloud messaging is a separate product than the database. That'd also explain why you can't find much. FCM is not necessary to use the database – OneCricketeer Dec 07 '16 at 02:25

2 Answers2

3

But what exactly is that?

Firebase Cloud Messaging (FCM) is the latest version of Google Cloud Messaging (GCM), which is Google's Push Notification Service.

It allows developers to send data from their App Server towards the Client App. For your case, you may be able to send over the desired data towards your Client App, by integrating your Database with FCM.

How does is interact with the database? How do I use it for listening and retrieving data?

If you are using Firebase Realtime Database, you may choose to integrate with FCM in such a way (referring to @FrankvanPuffelen's answer):

Sending messages to devices based on inserts into the Firebase Database will require you to run a trusted process, typically on an app server that you control. This trusted process listens to the database changes and then calls Firebase Cloud Messaging to send the messages.

For an example of how to send messaging from a node.js script, see my answer here: How to send Firebase Cloud Messaging from a node server?

In summary, you don't use it to listen for data. You can however use it as the medium to send the data from your Database towards your Client App.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • Can't I just instead use DatabaseReference in a class extending service ? What would be wrong with it? –  Dec 07 '16 at 22:30
  • 2
    @user6650650 Keeping a listener active for the Real-time database would also mean keeping an open socket on the user's device, which adds to battery consumption. See my answer [here](http://stackoverflow.com/a/40401062/4625829). – AL. Dec 07 '16 at 23:12
  • How about adding a single value event listener ( another interface which retrieves data only once , not on every update at database ) periodically , like every 30 seconds ? –  Dec 07 '16 at 23:30
  • 1
    That's still like *Polling*. See my answer [here](http://stackoverflow.com/a/38193888/4625829). – AL. Dec 07 '16 at 23:33
  • Also , how much battery cosumption would it cause? –  Dec 07 '16 at 23:36
  • That my friend, is a totally different concern from your post now. ;) – AL. Dec 07 '16 at 23:40
0

Since you are implementing Firebase for the first time, I'd recommend you to go through this and get a basic idea about it.

Broadly, FCM (previously GCM) is a Cloud Messaging service for your app. Most importantly, the Analytics and Realtime Database of Firebase are used.

Here is a link to their docs. Go through it for better understanding of Firebase and its implementation.

AlphaQ
  • 656
  • 8
  • 18
  • 1
    Firebase is a completely separate product than GCM. Firebase Realtime Database, that is, or the original product, Firebase Cloud Messaging is a rebranded GCM (and uses the existing technology called GCM) – OneCricketeer Dec 07 '16 at 02:23