What is the best way to make persistent in-app notifications with Firebase? - firebase

I learned how to use push notifications at Firebase. It works. As the next step, I want to add persistent in-app notifications, similar to Stackoverflow, Facebook, Instagram, etc
Currently we're using at Firestore and Real-Time database.
We have several use cases:
Users' content is viewed
Users receive a message from another user
Some system update about a new feature
Where should we keep these notification messages? Should it be in the same place where all original messages are stored? Or should each user have notification collections? Or are there some other ways to solve it?
I found a lot of stuff about push notifications, but I can't find anything how to handle regular persistent notifications.

Related

For a simple chat functionality why should I use something like Firestore when Push Notifications might be enough

In this simple scenario, why would someone choose to pay for a realtime service like Firestore for delivering latest updates, when a system based on Push Notifications can work as good?
If the user is not in the app, you can refresh the page as soon as the user taps the notification or opens the app. When the app is open you can refresh when a data notification is received.
What am I missing?
Why should anyone use something like Firestore when Push Notifications seems to be enough?

Flutter push notification using FCM by topic

hello I'm new to flutter and trying to get push notification in flutter , I have successfully subscribed the user to a topic and sent a notification through the firebase console by topic , what I'm trying to do is to have a button for admins in the app and when they post for a particular topic the users with that topic get the notification I have heard about cloud functions but I don't know exactly where to start ! i hope I made my point ! I have seen some videos that talks about type script but is there is any workaround this ?
The Firebase documentation names notifying the user when something interesting happens as an example use-case. From there:
Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
The function triggers on writes to the Realtime Database path where followers are stored.
The function composes a message to send via FCM.
FCM sends the notification message to the user's device.
To review working code, see Send FCM notifications.

How to use FCM topics for notification in chat app?

I am make chat feature with Flutter and Firestore backend.
Every message is new document in Firestore collection with UID and text field. Chat is 1:1 and random so no know who user will talk to before enter chat. DocID in chat collection are all auto-id.
I have read can use topics to manage send notification. This should be easier than use individual device fcm token.
Anyone know how to implement use topic for this random 1:1 chat app?
You can definitely use a separate topic for each 1:1 conversation, for example with the naming scheme I described here: Best way to manage Chat channels in Firebase. But there are some things to consider which, as Doug already pointed out in his comment, leads most developers to not solely use FCM for their chat apps.
For example: FCM topics are not secured. This means that anyone who finds out the topic ID can subscribe to it, and thus overhear the 1:1 conversation. And while you can generate topics that are hard to guess, you should not rely on not knowing the topic ID as a security mechanism.
Another reason to consider alternatives is that FCM messages are transient: once they are delivered there is no longer any trace of them. With your current Firestore implementation you can query the database to get all messages to show, while with a pure FCM implementation you will have to build your own database (if that is required for your app).
For these reasons most chat apps I know of use a combination of FCM (for push notifications) and an online database (for persistence) as their backend services.
I found Frank's comment really interesting about the "sorted userID composed key". I would probably use that as the chat key in the database (realtime/firestore), however, for the notifications I think I would still use a topic for each user - this way I would be able to avoid notifying the user who posted the message. If that wouldn't be an issue, then just go for just one topic per chatroom.
Also mentioning Frank, I would probably use extra keys in all topic names to make them really difficult to guess. (but add that later so you don't get distracted with non core stuff)
In this answer you have an example of how to post the notifications with a onCreate trigger to a topic (from a functions backend).
In the flutter code, you can use subscribeToTopic from the firebase_messaging plugin to start listening to a topic.
Note: if your app will support user log-off [probably it will :)], then you'll also have to delete the token in the device to avoid receiving notifications from the last logged user.

Does Ionic 2 support private & public Firebase notifications?

I'm thinking to work on firebase notifications in the last few days but I want to some answers for this few questions:
Does Ionic2 support firebase notifications ?
Will notification show in the top of screen when my application closed or it should be work in the background ?
Is firebase notification sent on time or it may take long time ?
If I assumed Ionic 2 can send private notifications (to my device only), so can I send public notification for all application users ?
If the answer of all questions "yes", wish you give me any useful links that may help me (if you have a time)
In addition to Gabriel's answer:
The default behavior for notifications is for it to show on the top part of the screen. I haven't worked on Ionic before, but AFAIK, the behavior should compared to Android.
With that said, you should refer to the Handling Messages part of the FCM docs.
There are factors to consider that may affect the time it takes for the device to receive a message, but as FCM's behavior, it would try it's best to send the message, as soon as feasible (see my answer here).
Gonna itemize this further:
Specific device only - Yup. When sending the message, just specify the registration token(s) that should recieve that message.
All devices - Yup. You could use the Firebase Notifications Console. But if you intend to send the message by using the REST API, you have to make sure that the devices are subscribed accordingly to your custom global topic.
About the device-to-device Gabriel mentioned, I think it should be worded as server to specific device(s).
Yes it does, and it works fine with FCM (Firebase Cloud Messaging, aka GCM - Google Cloud Messaging). It supports subscribing to topics and device-to-device notification via HTTP client. The topics subscriptions and device-to-device both require FCM plugin.
Yes, they'll appear on top of your device screen as any push notification, you'll just need to code how it'll behave after receiving it, like execute something if the notification is tapped, or do something if it's received and the app is already open (foreground), it's all up to you.
It's sent on time, i've never had delay problems, the longer it took to send the notification and i receive in my device was 10 seconds. But as far as i know it "stores" your notification for 24 hours, so if the servers go down or something happen the notification has a life time of 24 hours to be sent.
You can, as said in first answer, subscribe your users to topics. Let's say you want a topic to sent message so all users can receive, you can have an all topic, or have one only for logged users, one only for users who bought products, you can even get all users tokens and send one by one, but it's much better having them subscribe to topics.
Topics notifications can be sent via the Firebase project console. Device-to-device is sent by your app code.
To send device-to-device you'll need a token, it's provided by the FCM plugin (link above) and you'll need to store this token somewhere, like in your user node on Firebase. I usualy use the user device ID provided by the Device plugin to have his token, since the user can have multiple devices and log in all then, this'll prevent token overriding.
I don't know good tutorials about configuring FCM with Firebase, if i find something i'll edit the question and add it for you, but stick to the ionic native link and the FCM plugin github page.
Hope this helps.

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!

Resources