Quick HowTo: FCM (push notifications) with nodejs

How to setup firebase cloud messaging with nodejs

Installing FCM

npm install fcm-push

Using FCM  

var FCM = require('fcm-push');
var serverkey = '<insert-server-key>';  
var fcm = new FCM(serverkey);
var message {  
    to : '<insert-device-token>',
    collapse_key : '<insert-collapse-key>',
    data : {
        <random-data-key1> : '<random-data-value1>',
        <random-data-key2> : '<random-data-value2>'
    },
    notification : {
        title : 'Title of the notification',
        body : 'Body of the notification
    }
};
fcm.send(message, function(err,response){  
    if(err) {
        console.log("Something has gone wrong !");
    } else {
        console.log("Successfully sent with resposne :",response);
    }
});

Getting your server key-

- Go to firebase console
- Select your project
- Click on the 'gear' icon
- Go to Project Settings
- Go to Cloud Messaging
- Select the server key.

Testing your code with a sample app

You do not need to make an app to test your code. Here’s a quick guide on testing with the quickstart app.

  • Create a project in the firebase-console
  • Download the quick-start app for cloud messaging from here.
  • Download the services.json file present in the Project Settings of the project that you have created.
  • Add the file above file to your android application following the instructions from here.
  • Install the app to an appropriate device.
  • Click on ‘Log Token’. It will log the device token-id in the terminal.
  • Insert this obtained token replacing ‘insert-device-token’ in the above snippet of code.
  • Close the app in the device, and then run the code.

Add backend APIs to your apps in minutes with the Hasura platform. Take it for a spin here: https://hasura.io

This post was originally published on 20-June, 2016


Hasura is an open-source engine that gives you realtime GraphQL APIs on new or existing Postgres databases, with built-in support for stitching custom GraphQL APIs and triggering webhooks on database changes.


Hasura

Hasura

The Hasura GraphQL Engine gives you realtime, high performance GraphQL on any Postgres app. Now supports event triggers for use with serverless.

Read More