FCM downstream messaging using Node.js


FCM downstream messaging using Node.js

In our previous article we had a discussion on Android Push Notifications using FCM. That was how we receive messages and show in front-end or device. In this article we we will discuss how to send downstream messages in Node.js using fcm node. But before that let’s see how we can use firebase console to send messages to devices

To send messages from Firebase Console
Steps are :

  • First, select your project.
  • Then click Notifications from left side pannel.
  • A list of messages you sent before will appear here. If this is your first time it will be empty.
  • Click on NEW MESSAGE button.
  • An notification compose window will appear from where you can send messages directly to devices. See screenshot below.
  • Fill up all the necessary fields and hit SEND MESSAGE button. You can also SAVE AS DRAFT your message to send later.

The compose window will appear like

fcm from console

N.B. : You can also go to compose window directly if you already have your Project ID. Sample URL would be https://console.firebase.google.com/project/<Project-ID>/notification/compose

That was all about sending downstream messages from Firebase Console. Now what if you want to send it from your app’s server? That is this article all about. Yes, You can achieve it through different server languages like PHP, Node.js etc. We would like to talk about how to do it in Node.js server.

To make this work we have used following things along with Node.js –

  • Express – A Node.js framework for routing or adding middleware to your app.
  • FCM-Node – An npm library that supports group messaging, parallel calls, topic messaging and many more. It has support for ios also. You can see details here.

Installation

We are assuming you have Node.js & npm installed in your PC.

To install express globally run the following command in your terminal

And for fcm node you need to run in your project directory –

Now We will define our app.js where we will set and start our server

In the above have defined our port, added express, and file serverRoute.js for routing our app.

FCM Node at work

Let’s take a look at our serverRoute.js and use of fcm node

*To get token from android device read our previous article

For more details of the keys,json fields and their purposes we have used to send fcm notification read this instructions.

We have configured all our server related works. It’s time to run your server. Just issue the following command in your project folder.

You will see ‘server running on 4747’

To test receiving notifications

  • Go to your browser.
  • Hit http://localhost:4747/sendFcmNotification (As this is a GET request).
  • Wait a little for notification to arrive in your device.

You will receive notification in your device looks like picture aside.

Device Notification using fcm node

Feel free to comment or ask question if there is any query. If you liked this article share it. Subscribe to our newsletter for more cool stuff.


Author
Abhishek is a developer cum blogger working more than 4 years. He loves programming especially open stack technologies. He has decent knowledge in Android development, Wordpress, MongoDB, Node.js and so on. Beside this Abhishek finds himself busy in painting, front-end designing.

Follow Abhishek


Previous


Android FCM Push Notifications
Android Push Notifications using Firebase Cloud Messaging (FCM)

Next


Android GPS automatically
Turn on GPS automatically on Android (Programmatically)