I have created an app which uses firebase real-time database and storage. My app allows users to message each other, but I am not sure how to notify the user when they have received a new message. I store all of the messages in one node called messages. I then store the message relationships in a different node called "user-messages". How do I monitor the "user-messages" node to alert the user with a notification that they have a new message? I am familiar with observing event types and such, i.e.: .Value and .ChildAdded, but I am not sure how to trigger an event to send the user a notification.
edit: gave a little more clairity.
So I accidently did not see that Firebase can use Google Cloud Messaging which seems to be able to do what I want. For future reference firebase has put the IOS setup here :Firebase Cloud Messaging SetUp
Like you mentioned, you can use child_added event to monitor whenever a new message is added to user-messages and firebase will call your event handler whenever a new child is added.
ref.child('user-messages').on('child_added', (snapshot) => {
// Read the message and send notification here
})
You may want to update the user-message as well after it has been handled so that you don't end up sending duplicate notifications when your app restarts.
What I did was set up an XMPP server with Ejabbered in order to conduct notifications. Hope the following helps.
https://firebase.google.com/docs/cloud-messaging/xmpp-server-ref
https://firebase.google.com/docs/cloud-messaging/server#implementing-the-xmpp-connection-server-protocol
Related
We are able to send currency via the Custom data parameters in Firebase Cloud messaging (see SS below).
But if the user does not tap the notification "YOU RECEIVED 15 FREE GOLD!" then Firebase.Messaging.FirebaseMessaging.MessageReceived is never fired and the user never gets their currency. So the user may see the notification, and then be disappointed the next time they open the app and there is no gold.
Is there a way in Firebase to compose the notification so that the notification can be accessed even if the user did not tap the notification to open the app?
Without tapping on notification your application not launch and custom data you sent through notification is not received to the application. So sending custom data through firebase push notification is wrong choice in this scenario. You should sent custom data whatever you need through API is always good choice. You can also use firebase database and in that you can send custom data using nodes for respective user.
What I am trying to do is basically to automatize the FCM service, so I don't need to create the messages and schedule its action from the firebase messaging panel, but instead use SQL database will create that instance or schedule that notification automatically to be send at a certain hour. (Example: a delivery man ends its shift and a Store procedure register the hour so the supervisor knows the status of the route via the notification, something like the amazon delivery tracking works to notify the status of your delivery). What I want to know is if there is any way to integrate SQL to create those messages so firebase can send them. And if there is a way, how it works.
I would write a trigger in my backend service so that when certain a database table/column changed or some query is executed, it will trigger an HTTP request to send a FCM message.
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send
I'm very new to Firestore and trying to understand how the real-time updates work. I'm using something like this now to get the updates from Firestore:
db
.collection(Collections.session)
.whereField("participants", arrayContains:userID)
.addSnapshotListener { querySnapshot, error in
I noticed that the listener block is not getting fired when the app is in the background, but only when it's brought back to foreground.
Is there a way to get the update when the app is running in the background too? Maybe somehow send a push notification or something?
Any kind of help is highly appreciated.
Is there a way to get the update when the app is running in the background too?
Since backgrounded apps are eventually killed by the OS, you don't have a way to run a listener reliably when the app is not actively being used by the user.
You are correct in that the only way to (reliably) notify your app of some change in your backend is to send a push notification.
A very common approach is to use Cloud Functions to write a Firestore trigger that gets invoked when a document of interest is created, updated, or deleted. You can use this to write backend code that uses Firebase Cloud Messaging and the Firebase Admin SDK to send a notification to your app with a payload that tells it to respond to that change.
Push notification is not a reliable way to do this unfortunately. that backgrounded apps may be killed by the OS, if needed, but not always. The source of termination can be the OS or the user. If the user terminates your app, there is no way to receive a push notification.
I am creating a framework that can receive custom (a certain data model) messages from firebase. The framework is going to be implemented to receive notifications that are not related with the app but with other stuff.
So the framework is going to handle all the display issue by translating the data received and create a notification as it is indicated (I have some flags in data receive that indicated if I should use an image or attach an icon .. etc).
So I did some research on how to receive messages from FCM.
In first instance, I found direct channel that allows to bypass APNS, the problem is that, this only works with the app in foreground.
I indeed create a test project in firebase, a single view app, register my app in firebase project, set info.plist, configure , and send a notification to my app and It worked (just when the app was open).
Then I looked for another choice, and I found APNS. Skiping all the process for validation between firebase and APNS. I found that when you have all set up (and you put all initialization in didAplicationFinishLaunching) your app is able to receive notifications from firebase (Through APNS) when applicaton is in background.
But, notification received (the one that gets displayed) is just for you to tap over it and then it will fire up the app again an only then you will receive the whole data in aplication:didReceiveRemoteMessage method.
My question is, is there a way I can get this custom messages even in background and when received I can display a notification with the content of the whole message?
Yes,
Fortunately you can do that but for that you need to send Silent Notification which will let you process the notification in background and schedule local notification to trigger it with you desired data.
I'm using FirebaseUI to populate a table from the Database, hence, it also listens to changes, e.g. when a child is added, it automatically gets inserted into the table.
Now I'm trying to implement a notification upon that exact even, meaning when there is a new child added to the DB, there should be a notification that a new child has been added.
What is the best practice to do so?
I'm aware that there are Remote Push Notifications (what Firebase offers through its console), but this here is something else right? I guess there is a way in which the app itself notifies the user when a new child is added, but what happens when the app is closed?
With the new feature Cloud Functions, you can do this without any need of an external server.
See my response there for more details : Firebase notification on child added/modified
You're looking for Firebase Cloud Messaging, which is what Firebase Notifications is built on top of. Note that you will need an app server to run the code that listens for the changes to the database and then calls FCM to send the notification to the user.