I found two possible approaches to deliver notifications after some changes in the Firebase real time database (for example in a chat application):
It is possible to use Cloud Functions for Firebase as explained in this blog post.
I have also found here another simpler approach using just an android service that listens for changes in the database.
I would like to know what are the pros and the cons of the two approaches before trying to implement one of them and since the second one seems much simpler than the first one.
The android service
solution described in https://www.codementor.io/sundayakinsete/firebase-real-time-notifications-app-to-app-opkwbo6ba has serious limitations:
- it doesn't work when the application is not running (remember that when the user puts your app in background the system could decide to terminate it to free device memory)
- it uses extra battery, RAM and network data, to keep the service actively listening for remote database changes
- each device with the app running, even in background, would count towards the limit of 100,000 simultaneous connections to the database.
- it does not work on iOS
on the other side, if you use Firebase Cloud Messaging (via Cloud Functions or a custom server):
- you can receive notifications even if the app is closed
- you don't consume extra battery or cpu
- you can use the database limit of 100,000 simultaneous connections for the users that are actually using the app in foreground.
- it works on iOS and Web