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-notification

v2.0.0

Published

Push notificaton for ios and android with nodejs

Downloads

3,924

Readme

The fcm plugin for push notificaton on Android, ios and web.

fcm-notification

New reliesed of firebase team with admin.messaging() to send push notification to iOS, Android and web, March 21, 2018.

Usage

We tried to make this plugin as user (developer) friendly as possible, but if anything is unclear, please submit any questions as issues on GitHub: https://github.com/smartnav/fcm-notification/issues

###Install from NPM

$ npm install fcm-notification --save

Examples

**Before starting you need to download privatekey.json file from ** This link

Check Steps:

  • Go Choose a project to continue to the Firebase console - If already have then click on it otherwise create new one.
  • Go to Service account tab and generate new private key (In node.js)
  • Add this file in your project's workspace
  • Import that file with a require('path/to/privatekey.json') style call and pass the object to the FCM constructor

Start:

Send to single token

var fcm = require('fcm-notification');
var FCM = new fcm('path/to/privatekey.json');
var token = 'token here';

	var message = {
		data: {    //This is only optional, you can send any data
			score: '850',
			time: '2:45'
		},
		notification:{
			title : 'Title of notification',
			body : 'Body of notification'
		},
		token : token
		};

FCM.send(message, function(err, response) {
    if(err){
        console.log('error found', err);
    }else {
        console.log('response here', response);
    }
})

Send to multiple tokens

var fcm = require('fcm-notification');
var Tokens = [ 'token1 here', '....', 'token n here'];
var FCM = new fcm('path/to/privatekey.json');

var message = {
  data: {
    score: '850',
    time: '2:45'
  },
  notification:{
    title : 'Navish',
    body : 'Test message by navish'
  }
};
FCM.sendToMultipleToken(message, Tokens, function(err, response) {
    if(err){
        console.log('err--', err);
    }else {
        console.log('response-----', response);
    }

})

Send to a topic

FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. But before this we need to subscribe to topic.

Subscribe to topic

var fcm = require('fcm-notification');
var FCM = new fcm('path/to/privatekey.json');
var tokens =[ 'token1 here', '....', 'token n here'];

FCM.subscribeToTopic(tokens, 'TopicName', function(err, response) {
    if(err){
        console.log('error found', err);
    }else {
        console.log('response here', response);
    }
})

Unsubscribe to topic

var fcm = require('fcm-notification');
var FCM = new fcm('path/to/privatekey.json');
var tokens =[ 'token1 here', '....', 'token n here'];

FCM.unsubscribeFromTopic(tokens, 'TopicName', function(err, response) {
    if(err){
        console.log('error found', err);
    }else {
        console.log('response here', response);
    }
})

Send push to topic

var fcm = require('fcm-notification');
var FCM = new fcm('path/to/privatekey.json');
var TopicName = 'TopicName';


var message = {
  data: {
    score: '850',
    time: '2:45'
  },
  notification:{
    title : 'Title name',
    body : 'Test body..'
  },
  topic: TopicName
};
FCM.send(message, function(err, response) {
    if(err){
        console.log('error found', err);
    }else {
        console.log('response here', response);
    }
})

Send push to multiple topics

var fcm = require('fcm-notification');
var FCM = new fcm('path/to/privatekey.json');
var Topics = ['Topic one',  '..',  'Topic n'];


var message = {
	  data: {
			score: '850',
			time: '2:45'
	  },
	  notification:{
			title : 'Title name',
			body : 'Test body..'
	  }
};
FCM.sendToMultipleTopic(message, Topics,  function(err, response) {
    if(err){
        console.log('error found', err);
    }else {
        console.log('response here', response);
    }
})

Defining the message

It is possible to set android, apns, webpush and notification fields on the same message. FCM service will take all specified parameters into account and customize the message for each platform. However, a message must contain exactly one of the token, topic or condition fields. It is an error to specify zero or multiple fields.

Android-specific fields

var message = {
  android: {
    ttl: 3600 * 1000, // 1 hour in milliseconds
    priority: 'normal',
    notification: {
      title: '$GOOG up 1.43% on the day',
      body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
      icon: 'stock_ticker_update',
      color: '#f45342'
    }
  },
  topic: 'TopicName'
};

APNS-specific fields (IOS)

var message = {
  android: {
    ttl: 3600 * 1000, // 1 hour in milliseconds
    priority: 'normal',
    notification: {
      title: '$GOOG up 1.43% on the day',
      body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
      icon: 'stock_ticker_update',
      color: '#f45342'
    }
  },
  topic: 'TopicName'
};

WebPush-specific fields

var message = {
  webpush: {
    notification: {
      title: '$GOOG up 1.43% on the day',
      body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
      icon: 'https://my-server/icon.png'
    }
  },
  topic: 'TopicName'
};

Putting it all together

A message may contain configuration parameters for multiple device platforms. This means it is possible to include android, apns and webpush fields in the same message. The FCM service customizes the message for each target platform when delivering. The following example shows how a notification has been customized for Android and iOS platforms:

var message = {
  notification: {
    title: '$GOOG up 1.43% on the day',
    body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
  },
  android: {
    ttl: 3600 * 1000,
    notification: {
      icon: 'stock_ticker_update',
      color: '#f45342',
    },
  },
  apns: {
    payload: {
      aps: {
        badge: 42,
      },
    },
  },
  topic: 'TopicName'
};

In the same vein, it is possible include both data and notification fields in the same message.

License

MIT