Trigger FCM using Node JS Server | How to send push notifications using Node JS and Firebase?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to see how can you create Firebase push notifications using a node.js server uh under the hood they would be using fcm Firebase Cloud messaging and uh we'll trigger this using a node.js server so we were recently asked on our community Discord so we have a Discord if you aren't a part of it you should maybe consider joining it so this is recently asked a question like uh how can we make automatic push notifications in our flutter app and also can it be done without you know upgrading to the place plan so uh generally if I were to do it all like recently I worked on a project that required me to add push notifications I did it using Firebase Cloud functions but for that you need like credit card information that you have to put in have a cloud function run that would send out notifications but you can also do it on the spark plan so like let's say it's for a smaller project or you're just testing something out so you can create a node.js server which would have admin permission and what I have done in this example is create like a post request to it and then the node.js server would trigger notification that would be sent out and shown inside your application so let's do that let's build that today okay so this is uh the flutter application that I have created for this particular tutorial there are two things that I have done already one is setting up Firebase so if you do not know how to set up Firebase I have a video on that you can watch that using the I button it should be somewhere on the top and after you're done setting up Firebase what we need to do is also add like another package which is this Firebase messaging so you can run the command flutter Pub add Firebase messaging and it would add that package to your pubspec dot EML so we only have these two like the core and core is required to set up Firebase and then we have the messaging package once we are done with that like I only have a single file uh what I am doing is I am handling background messages so even if I close my application I should be able to see the notification uh it does not do much like the main function has nothing in it uh inside this we only do like the Firebase setup and this is the fcm token so every device like has a token uh generally if you have if like I want to send out a notification to my mobile device I can you know take the fcm token of this device put it on the Firebase console and send a notification so that is like the manual way so I'm just printing out the fcm token here and on the main app like which is the default widget that was there I am just showing a text button so the text button also doesn't have anything in it it just says try post and alert so this is the simple code nothing much to do in it except like Firebase setup and Firebase messaging uh the background messaging this part might be new other than this nothing else is there inside this application we need a Firebase project that we would be using uh what would happen is like we'll set up Firebase server SDK so you can also uh check out the documentation Firebase server SDK how can you add Firebase admin to your server uh Firebase admin supports like supports different languages so there's a JavaScript node.js Java Python and C sharp so let us for our particular use case we'll create it using node.js um we'll send a post request to This node.js Server which will then communicate into Firebase and for that it needs to be an admin so we'll start with that first step is that you have node.js on your system so you can go over to download node.js depending on the operating system you have you can download an installer and set it up it should be easy next uh we can open up the terminal and I'll zoom in a bit we'll create a new directory so mkdir no tip for the purpose of this tutorial I can call this project either note F1 notify and once I have this folder I can navigate to it once we are inside this folder I need to run the command in PM init to initialize like a node.js project notify description you can give it some keywords author and you can hit enter to finish this next we can open this up in vs code so right now we have no diff open here and we can return to Firebase now what we need to do is like we need to go over to the project settings go down to service account and here uh we'll see the necessary steps to add like Firebase admin SDK to node.js now we can inside this node.js project create index.js file because during setup that's what we named it so we named here index.js so you can create that once we have added this we need to generate a new private key and we can hit generate key and it should download a Json file for us so we can take this JS and paste it here so we have pasted the Json file here and now uh if you go back to the documentation itself add Firebase admin um you can go down to send Firebase Cloud messaging messages here and I'll also put all these links in the description for you to see and what you'll see here is there is another page called authorize send a request so when we want to you know send out a message we need to authorize it for that we have like we need to add the credential so we have already added this Json file but the path of this file needs to be added uh as a environment variable which would be called Google application credential and that way like you know it will authorize your request and it will be able to process it so for like if you are on Mac or Linux you can simply run this command to add it to the environment variables so I'll paste it here and I'll take this file copy path replace the path here [Music] so this is the complete path and I'm going to open up the terminal here run this command so once I run that command what would happen is now uh this Google application credentials variable environment variable would be set to this path that way when I run this particular application it would know like where to get this file from to you know initialize up so we need this credential if you will see here we need that credential to like initialize application and this is an important step so once you're done with this like you are set up and what I'll do is right now is like I'll um I'll show you the server that I've actually worked on so uh there are a few things that you know you have to change here one thing would be like um I'll instead use type module and I'll have to replace this with import admin from Firebase admin all right so I'll initialize app with the credential application default the path that we added right here would be picked and it would you know authorize our application so we'll add two more Imports one is uh Firebase admin messaging and the other is Express so that we can create API endpoints one more thing I'll do is create a EnV file and inside this EnV file I can do const and add like this Google application credentials here equal to and set this to this relative path here of the service.json now it is not recommended that you also make this public so this has to be saved privately somewhere you do not have to make it public uh inside your environment variables this Google application credential environment variable should have the path of this file since I am new to node.js uh like for now for this tutorial I would just put it here add a relative part to it and use this environment variable inside like my project next uh I'll run my server to listen on 3000 port and print out server started and it's done and I'll also create an instance of Express and make it use like application Json the important part here would be to create a post so when we hit Dash send the end point it should send out a notification so let's work on that what we'll do is we'll create our message so if you go over to the documentation uh for Firebase admin messaging let's go down to server uh environments and send message so this send message page has how can you send out uh these messages using your server and it has like options to create so we can create a message like this with some data like score and time and we can also create notifications like with the title and a body so you have your title you have the body of the notification here so that is what we are doing here we have created a message which has this notification info a title and a body and we are also putting in the token so this token is the fcm token of the device that I have here what I had done was like in my flutter app I had printed it out copied and pasted it here now if you if you like want to send to this particular device like for this demo uh you can do that but if you want to send to a device of your fcm token let us say um this app is in production so what I can do is I can get that token so received token in the request so whenever I'll make a post I can add some body of data to it and that could have this fcm token I can get that fcm token here and instead of using like a hard coded value here I can use that receive token there so these are like all modifications um which like you can do you can take that receive token put it here and send out a notification to that it could change and you can also send out notifications to multiple tokens so you can have like a registration tokens list you can do up to 500 device registration tokens so you can send out a notification to 500 devices at once once we create our message uh we'll use get messaging that we had imported earlier to send out a message and if it successfully does we can uh set the response status to 200 and also send out like a response which says like successfully sent message uh then lastly if it does not so we can cache the error send a status of 400 and also send out the error that is happening so that is all uh that you need to do what I'll do is I'll also add the code of this to like maybe adjust that you can copy from and add to your server I wanted to quickly explain the steps with the service account how to use that so let us also see how it is this deployed for deploying This node.js Server now you can run this locally but you want to like send out notifications on the go so you can deploy it on like a free service like dashboard.render it's very simple here you can create a web service and select the repository so like I had this no tip so I can simply hit connect and it showed you now connect to it like take this code deploy it to like a private repository on GitHub connect it here give it a name and um you all you have to do is like give it a name select the server close to you and then under build command you will type in node sorry npm install and here node the name of the file that you want to run and then you can select the free tier and create a web service so I already have one deployed like I have this note if already deployed the code for this is available here so I'll close this notify and show you the code here quickly the code for this is available here as you can see like I have this service account here and it is almost a similar code I'm sending out the message and that would be it so what this means is like let me have my device uh open on the side here I have my device here I have my notif instance working here I can open up something like Postman I post mine here go down to my work space and hit new http and here I can enter the URL so if you remember we have to hit this URL so this URL comes from here where we have hosted our node.js server on the web and we have to type in send which is um the API endpoint that you had created and we have to hit it with the post request once I do send it will send request so once I hit send I'll get uh successfully sent and on my mobile you will get that notification at the top and it says now so it was delivered just now so that is it for this tutorial now let us talk a few modifications here one thing that you can change here is like uh we have this app dot post which is our endpoint you can get not only this uh token like the token of the device that you have to send this notification to in the message but you can also get like the title or the body of the message here maybe an image if you want to send that out once you have this data you can send out a notification very much like this and you can also maybe return a response like it was successfully sent where can it be used so if you have to like do local notification I think there are packages for that that you shouldn't be using but let us say you're building a social media app where I send out a message to someone so at the time of sign up I can save everyone's you know fcm token and when I want to send a message to someone I can take their fcm token um from their user data and send out a particular notification to them with maybe the message that I am sending so let us say you're building a WhatsApp clone I send out a message to a friend to you maybe and I can take your fcm token I can put out a notification to it using the message inside the body and the name of the person is ending so in that case it would be Aditya inside the title so I hope this tutorial helps you get started now I would have I would try to you know make this even better but I wanted to ship out this video quickly to give you like a brief idea of how this is done and hopefully you can get out there build out your own versions of this particular node.js server and if you can please do share it with me thank you so much for watching this video I hope you liked it uh and please do join our Discord server we have fun discussions like this uh all the time thank you so much
Info
Channel: Aditya Thakur
Views: 13
Rating: undefined out of 5
Keywords: fcm, firebase, firebase messaging, node js, node firebase admin sdk, flutter push notifications, in-app messaging, automatic flutter notifications
Id: J8j_jzWPRtw
Channel Id: undefined
Length: 17min 32sec (1052 seconds)
Published: Sun Jul 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.