Local Notifications vs FCM: Which is right? - firebase

I have a fairly straight forward Flutter app which incorporates some "social" features, such as the ability for users to add other users as friends.
When a friend request is "send", a record is added to the Firebase to represent the (pending) friendship. I would like the user "receiving" the friend request to get a notification.
I've looked up a dozen or more posts on using local notifications and FCM, but all I can find are bare-bones PoC style examples. I'm at a loss to understand which methodology is correct for this situation.
Can FCM somehow listen for changes on the database, so when the friend request record is created, it would then push a notification? Or should the receiving user's app be listening for changes to the friend requests and push a local notification?
I'm at a loss for where to start.
Thanks in advance!

The answer is "You have to make your backend listen for changes on the database and send FCM".
If you are using firestore as your database, you can use cloud functions to listen to the changes made to your database. You can read more about it here:
https://firebase.google.com/docs/firestore/extend-with-functions
If you are using a backend of your own, you can make the backend send FCM to a user using the firebase admin sdk for whatever language you are using.

Related

Secure communication between Flutter App and Web PHP

I'm learning Flutter myself and need some expert guidance on the right way to do what I want.
I would like to allow the user of my Flutter application to securely send some sensitive data (e.g. email, phone number, name etc.) to my PHP based website (with REST API) and then, after some time, I would like to send a reply (e.g. . text) from my website to the sender's device and displaying it on the User's screen.
I suspect I should use Firebase Cloud Messaging for this - am I right?
I found tutorials that explain how to send data from Flutter-> FCM and from PHP(curl)-> FCM. But I can't find the tutorial explaining how to send data from Flutter-> FCM-> PHP?
Firebase Cloud Messaging is typically used to send messages from the server to the application, when the user may not be actively using it. While it can be used to send messages from the client to a server, it is much less common for that.
That's because calling a PHP API from your Flutter app is no different than retrieving any data from the internet, and can be accomplished in a multitude of ways. One of the simpler approaches is documented in Flutter's fetching data from the internet page and uses the http package.

Can I use FCM’s onMessage functions to create a live-chat application?

I’m making an app using react with firebase, and a large part of my app involves group communication. Currently I have FCM sending notifications to people within the group whenever someone types a message into chat. Can I use onMessage to load messages in real time for all group members to see, without needing to refresh the page, or do I need to set up firebase real time database ?
Any help/advice is greatly appreciated !
I would recommend to store the messages in one of the Firebase databases. For multiple reasons. You can only send FCM messages from the backend so you need a trigger for that. Idealy when a new messages is added to the database. If all users for a group listen to messages in a group chat they would see them when you setup a realtime listener. This would work much faster and more reliable than using FCM for realtime data. FCM is idealy used to notify users when they are outside of the app or inside the app but not in the chat by using onMessage. The UI should rely only the databse data and not FCM.

Should I deploy Firebase Cloud Messaging server myself?

I've read the document about FCM, but I wonder if I should deploy a FCM server myself. Does the google cloud platform not provide FCM server? Or do I must implemented my own FCM server refering to the official reference implementation, gcm-playgroud?
It depends. (How's that for an answer?)
By "server", I'm assuming you mean, "Code I will be running on the server level to communicate with the FCM service". And honestly, the answer depends on whether or not you need extra functionality that's not supplied by the Firebase Notifications panel.
You can do a lot with Firebase Notifications, including sending scheduled messages, or sending messages to individual users, topics, or Audiences. But that is work that is done manually. If you just want to send the occasional promotional message to your users, you shouldn't need a server.
On the other hand, maybe you've got a messaging app and you want to send a notification to your user anytime they receive a new message. Well, that's clearly something that needs to be automated, and that requires some server-side code. So for that, you will need to set up your own server that communicates with FCM.

Dynamic Push Notifications

I know that Firebase has recently added support for Push Notifications and while this is a great thing, I only seem to be able to send push notifications manually via the Notifications Panel.
What I'd like to do is to send push notifications within a user scope...Let me explain that. Each User in my App has an account and then each user can join a group. Within this group the user can perform tasks and has a list of chores to do. Now when certain tasks are due for example I want to remind the user of doing it with a push notification. For 1-10 I might be able to pull this off manually, but is there a way to dynamically based on the data in the Database send out Push Notifications?
I know that certain Push Notifications can be created using the Analytics tool such as "Hey you have not visited for 3 days, please come back whatever"... but I'd like to register push notifications such as "I just created a task, this task needs to be done within 3 days. Remind me in 3 days if the task is still not done".
Is this possible with Firebase or do I need to have my own server connecting to Firebase and handling those events?
-xCoder
You need to implement FCM in your client and in a server. Let me put this straight:
First, you need that your client, or app, to register into FCM and get a FCM token that will be used to identify that device uniquely.
Then, store that token wherever you like. It can be into firebase database or other server you may like. I recommend you to store it into firebase if you are using it as a database for your users; that's my case.
Also, you need to implement a http or xmpp server in order to send FCM messages to your registered devices containing the data you are interested in. For example, you can implement a Google App Engine endpoint (can be done with Android Studio and Java) that is quite simple or a NodeJS module, depending on your preferred language.
If you are using Firebase as database you can connect from your server with the appropriate SDK and get the FCM tokens you want from your users, and then send the message to those with data. Don't forget to secure your serve.
The way you implement your server algorithm to send FCM messages depends on your app purposes.
Hope it is clear enough for you. Also you can find all the documentation with a short video that explains the general structure here: https://firebase.google.com/docs/cloud-messaging
You can use cloud functions to trigger on any create, update or delete operation in your database and in the trigger event, you can choose to send in FCM push notifications to the devices of your choice.
Here is the documentation regarding the use and structure of a cloud function: https://firebase.google.com/docs/firestore/extend-with-functions
Hope this helps!

is there something like a webhook on firebase so that I can increment/decrement presence

I need to sync the number of online users in each chat room to my elastic search repository (the one that searches for rooms).
Is there a way to attach a webhook server-side onto the connect and disconnect events that fire in firebase so that I can inc and decrement the user counts in my elastic search records?
I would probably use a node.js client for this. That way you can write your client against the same JavaScript SDK that you're already using.
In that client you would subscribe to the same events on the same paths as you would do in a regular front-end client. But instead of updating the UI, you'd just update a node in Firebase itself.
So a regular Firebase client would:
accept user input and send it to Firebase
listen for changes in Firebase an update the user's screen
This node.js client would instead:
listen for changes in Firebase and update the data in Firebase based on that
As Kato mentions: Flashlight is a perfect example of such an application, synchronizing data from Firebase to ElasticSearch and fielding ES queries by queueing them through Firebase.

Resources