Code Monkey home page Code Monkey logo

fcm-notification's Introduction

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

fcm-notification's People

Contributors

diavrank avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.