npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fcm-node

v1.6.1

Published

A Node.JS simple interface to Google's Firebase Cloud Messaging (FCM). Supports both android and iOS, including topic messages, and parallel calls. Aditionally it also keeps the callback behavior for the new firebase messaging service.

Downloads

58,483

Readme

Warning: on February 2, 2017, the Firebase Team released the admin.messaging() service to their node.js admin module. This new service makes this module kind of deprecated

fcm-node NPM version

A Node.JS simple interface to Google's Firebase Cloud Messaging (FCM). Supports both android and iOS, including topic messages, and parallel calls.
Aditionally it also keeps the callback behavior for the new firebase messaging service.

Installation

Via npm:

$ npm install fcm-node

Usage

There are 2 ways to use this lib:

The classic one

  1. Generate a Server Key on your app's firebase console and pass it to the FCM constructor
  2. Create a message object and call the send() function

Classic usage example:

    var FCM = require('fcm-node');
    var serverKey = 'YOURSERVERKEYHERE'; //put your server key here
    var fcm = new FCM(serverKey);

    var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
        to: 'registration_token', 
        collapse_key: 'your_collapse_key',
        
        notification: {
            title: 'Title of your push notification', 
            body: 'Body of your push notification' 
        },
        
        data: {  //you can send only notification or only data(or include both)
            my_key: 'my value',
            my_another_key: 'my another value'
        }
    };
    
    fcm.send(message, function(err, response){
        if (err) {
            console.log("Something has gone wrong!");
        } else {
            console.log("Successfully sent with response: ", response);
        }
    });

The new one

  1. Go to your Service account tab in your project's settings and download/generate your app's private key.
  2. Add this file in your project's workspace
  3. Import that file with a require('path/to/privatekey.json') style call and pass the object to the FCM constructor
  4. Create a message object and call the send() function

"New" usage example

    const FCM = require('fcm-node')
    
    var serverKey = require('path/to/privatekey.json') //put the generated private key path here    
    
    var fcm = new FCM(serverKey)

    var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
        to: 'registration_token', 
        collapse_key: 'your_collapse_key',
        
        notification: {
            title: 'Title of your push notification', 
            body: 'Body of your push notification' 
        },
        
        data: {  //you can send only notification or only data(or include both)
            my_key: 'my value',
            my_another_key: 'my another value'
        }
    }
    
    fcm.send(message, function(err, response){
        if (err) {
            console.log("Something has gone wrong!")
        } else {
            console.log("Successfully sent with response: ", response)
        }
    })

Multi client support (thanks to @nswbmw)

const FCM = require('fcm-node')

let fcm1 = new FCM(KEY_1)
let fcm2 = new FCM(KEY_2)

Topic subscription on web clients

Web clients doesn't have a "native" way to subscribe/unsubscribe from topics other than manually requesting, managing and registering with the google's iid servers. To resolve this "barrier" your server can easily handle the web client's sub/unsub requests with this lib.

For more detailed information, please take a look at Google InstanceID Reference.

PS: For mobile clients you can still use the native calls to subscribe/unsubscribe with one-liner calls

Android
FirebaseMessaging.getInstance().subscribeToTopic("news");
iOS
[[FIRMessaging messaging] subscribeToTopic:@"/topics/news"];

Subscribe Device Tokens to Topics

var FCM = require('fcm-node');
var serverKey = 'YOURSERVERKEYHERE'; //put your server key here
var fcm = new FCM(serverKey);

fcm.subscribeToTopic([ 'device_token_1', 'device_token_2' ], 'some_topic_name', (err, res) => {
    assert.ifError(err);
    assert.ok(res);
    done();
});

Unsubscribe Device Tokens to Topics

var FCM = require('fcm-node');
var serverKey = 'YOURSERVERKEYHERE'; //put your server key here
var fcm = new FCM(serverKey);

fcm.unsubscribeToTopic([ 'device_token_1', 'device_token_2' ], 'some_topic_name', (err, res) => {
    assert.ifError(err);
    assert.ok(res);
    done();
});

Notes

Credits

Extended by Leonardo Pereira (me). Based on the great work on fcm-push by Rasmunandar Rustam cloned and modified from there, which in its turn, was cloned and modified from Changshin Lee's node-gcm

License

MIT

Changelog

1.6.0 - Multi client support - Thanks to @nswbmw for this feature 1.5.2 - fixed a bug where the send callback was being called twice - Thanks to @cesardmoro for this fix 1.3.0 - Added proxy capabilities - Thanks to @crackjack for this feature 1.2.0 - Added topic subscriptions management for web clients - Thanks to @sofiapm for this feature
1.1.0 - Support for the new firebase node.js sdk methods
1.0.14 - Added example file to quick tests 1.0.13 - Added a error response in case of TopicsMessageRateExceeded response 1.0.12 - Refactored the client removing the Event Emitter's Logic to fix concurrency issues. Using pure callbacks now also avoids memory leak in specific scenarios with lots of parallel calls to send function. 1.0.11 - <FIX> send function returning error objects when multicast messages (or individually targeted) returned both error and success keys on response message (even with error counter = 0 ) 1.0.9 - Updated Documentation 1.0.8 - <FIX> 'icon' field no longer required in notification 1.0.7 - renaming repository 1.0.6 - bugfix: send function was always returning an error object for multicast messages (multiple registration ids) 1.0.5 - bugfix with UTF-8 enconding and chunk-encoded transfers 1.0.1 - forked from fcm-push and extended to accept topic messages without errors