how to send background notification to client on chat web app - firebase

i am creating a web based chatting application with firebase and next.js. which is currently in development.
the problem i am facing is i want to have push notification for every new message received to user when the app in not in foreground. is their any thing to do with service worker?
i thought of using service worker to do so. but i don't know how to write service worker. i have stored user auth token in react context can i use it in service worker ?
i am fetching chats from messages array stored in firestore doc of chat between two people.

Related

I need to create a web app which need push notification without FCM/GCM or APN

I need to send push notification even the site is closed, So I've tried service worker and web worker even web socket but it didn't works fine. It need FCM(Firebase Cloud messaging) to transfer payload from server
,but my server don't support Nodejs so I can't use Firebase
I need a solution to create a custom endpoint to delivery payload to a client in the Background. So, I can push a notification from the Service worker instantly even the site is closed

Register device on Firebase on server side (Firebase Admin API)

Start Notes :
When a mobile application starts with Firebase enabled, the first thing that happens is Firebase creates a Token ID for the device on that app.
This requires the device to connect to the Firebase server, and register the device with all the required data about the device.
Unfortunately this cannot happen if the device has blocked access to the Firebase server.
Searches :
As I know, there is a Firebase Admin API on the server side, that can be used to create functions and probably one of them is to create a custom Token ID and register the device using that ID.
However, searching Firebase Docs, only creating a Custom Auth ID appeared
Create Custom Tokens
But this Token ID is not for the device, it is for the user on the device (i.e, Firebase Authentication API).
Goal :
What I want to do, is handle the entire Token ID process from the server side, so that even a Firebase blocked device can be registered on Firebase.
Question :
What is the method of creating a Firebase Token ID on the server that also registers the device as a user with its data (Device Name, OS, etc)?
Is there such a process? Or does Firebase exclusively register the device from the client side?
Side Notes :
I believe it is possible, as some applications that obviously use Firebase Messaging are available for use even when Firebase's access is blocked.
Examples : Discord, Telegram.
The token ID is managed by the client device. The backend and admin SDK are not involved in this process. It's the responsibility of the client app to send this token to you backend, if you need to target the app for use with Cloud Messaging. If the app is temporarily unable to reach your backend, you will have to retry sending the token until it's able to reach the backend.

Is Firebase Cloud Messaging indicated for social activity messages

I need to handle social activity events in my web app application developed using Vue JS framework and Firebase.
I want to show to the user “Facebook like” notifications when some activity happen in the application, for example:
“John liked your post”.
Is Firebase Cloud Messaging useful for this scenario? Or do I have to develop some custom solution from scratch?
If Firebase Cloud Messaging do the job, is it possible to send messages directly from user web client?
You should not try to send messages directly from the web client. FCM is intended to be used via a secure backend where your code runs to send messages. The reason a backend is needed is to prevent your private server key from being exposed to the world, which can cause security issues in your app.
You will have to arrange on your backend to determine when events occur that should generate messages, and use the Firebase Admin SDK to send those message, or work with the REST API directly.

Adding Mobile Number based login mechanism for auth user in firebase

Can we implement Firebase for an android app, where I am registering user using Mobile number (Similar to Whatsapp). Users will be sent a code by server which is entered by user in android app to validate the user mobile number and registering him on the server.
Question : Can I use the above method in conjunction with Firebase Auth?
I was earlier going to use MongoDB for my project, but since Firebase has SYNC capabilities, it will be a better choice for storing data. Another good reason is as below:
If a client loses its network connection, your app will continue
functioning correctly.
Every client connected to a Firebase database maintains its own
internal version of any active data. When data is written, it's
written to this local version first. The Firebase client then
synchronizes that data with the remote database servers and with other
clients on a "best-effort" basis.
Very NEW to Firebase, just came to know about firebase (through Google 2016 IO).
https://firebase.google.com/docs/database/android/save-data
Firebase hosting is not for server side processing.
It stores static assets of your website as a world-class high availability CDN. So websites hosted here loads very fast. Even in high-availability scenarios.
So you have to do processing at other server which then connects with firebase and stores userinfo in realtime database.
Firebase has put limits on userinfo to be placed in directly for users auth dashboard.
For detailed userinfo, firebase realtime db is the way to go(from your processing backend to firebase realtime db).
Further Reading: What kind of web applications are Firebase not ideal for?

Security rules for firebase-queue for handing advanced authentication

I intend to use Firebase Queue to push tasks for registering a user using his/her mobile number like Whatsapp for a mobile application. I am using a third party service to verify a user's mobile number. The verification process is a two step process
Send the user's mobile number to the third party service which returns back a request_id and sends a code to the user's mobile via SMS
Send the request_id and code entered by the user and verify it
This is like the first thing that I would do when the user open the mobile app for the first time after installing. Since the user is not even registered yet, there is no auth data for the user.
I want to ask what kind of security rules can I add to the firebase queues so that no outsider can add/remove tasks to the firebase queues.
I can also expose APIs on the server directly which the mobile app can use to perform the above verification but I am thinking that if there is a way to add some security rules to the firebase queues for such a scenario case also, then I would rather have the mobile app talk to only firebase.
I have created the following sample code
1.Queue worker
2.Client
Currently, anyone who knows my app location can add a task to the queue. Since, the user has not been authenticated yet, I cannot add any auth specific security rules to firebase. I was thinking of shipping the mobile app with an application secret and have something like this for the security rule for the firebase queue
{
"rules": {
"queue": {
"tasks": {
".write": "newData.appSecret === <appSecret>"
}
}
Here <appSecret> is what is shipped with the mobile app and each new task also contains the appSecret in it. Now, only clients who know the appSecret can add tasks to the firebase queue.

Resources