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.
npm install fcm-nodefcm-node 
========
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.
Via [npm][1]:
$ npm install fcm-node
There are 2 ways to use this lib:
js
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);
}
});
`$3
1. Go to your [Service account tab][13] 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
`js
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][14].
PS: For mobile clients you can still use the native calls to subscribe/unsubscribe with one-liner calls
##### Android
`java
FirebaseMessaging.getInstance().subscribeToTopic("news");
`
##### iOS
`objective-c
[[FIRMessaging messaging] subscribeToTopic:@"/topics/news"];
`$3
`js
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();
});
`$3
`js
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();
});
``Extended by [Leonardo Pereira (me)][3].
Based on the great work on [fcm-push][7] by [Rasmunandar Rustam][4] cloned and modified from there, which in its turn, was cloned and modified from [Changshin Lee][5]'s [node-gcm][5]
[MIT][6]
[1]: http://github.com/isaacs/npm
[2]: https://firebase.google.com/docs/cloud-messaging/server
[3]: https://github.com/jlcvp
[4]: mailto:nandar.rustam@gmail.com
[5]: https://github.com/h2soft/node-gcm
[6]: https://opensource.org/licenses/MIT
[7]: https://github.com/nandarustam/fcm-push
[8]: https://firebase.google.com/docs/cloud-messaging/concept-options
[9]: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/APNsProviderAPI.html#//apple_ref/doc/uid/TP40008194-CH101-SW2
[10]: https://firebase.google.com/docs/cloud-messaging/http-server-ref
[11]: https://firebase.google.com/support/release-notes/admin/node
[12]: https://firebase.google.com/docs/reference/admin/node/admin.messaging
[13]: https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk
[14]: https://developers.google.com/instance-id/reference/server#create_relationship_maps_for_app_instances
[15]: https://github.com/sofiapm
[16]: https://github.com/crackjack
[17]: https://github.com/cesardmoro
[18]: https://github.com/nswbmw