Git Product home page Git Product logo

node-fcm-hero's Introduction

Build Status Coverage Status

node-fcm-hero

FCM interface with super powers. Send push notifications to android/ios in a simple way

Get Started

Install

npm install --save node-fcm-hero

Usage

To use this lib, you have to import:

const fcm          = require('node-fcm-hero');
const { firebase } = require("./your.config.file");
const push         = fcm(config);

The config file must have this format:

{
  "firebase": {
    "cert": "/route/to/firebase-adminsdk.json",
    "url" : "https://your-project.firebaseio.com"
  }
}

The cert param has to be the path to yor firebase json file.

If you already have an initializated instance of firebase-admin module, you can pass directly the messaging function to initialize the node-fcm-hero. Ie:

const fcm          = require('node-fcm-hero');
const { firebase } = require("./your.config.file");
const push         = fcm(config, myFirebaseAdmin.messaging());

Send notification

To send a push notification you will need to call the send method. Ie:

const event = {
  token   : '',
  type    : '',
  payload : '',
  pushType: ''
}
push.send(event)
  .then(res => console.log(res))
  .catch(err => console.log(err));

Push notification type

You can set the pushType attribute of the event to define the kind of template to send. This pushType must be a string with the name of one of the custom templates you initialize to the project.

If you do not set any template or it didn't find in the type in the templates folder, the notification will fail.

You can make this work like an event Redux flow, passing the type and defining a template file per event you want to push.

If the event type don't macht with any template, the node-fcm-hero will return a unsuccess json object:

{
  "success": 0,
  "error"  : "No template found for this event"
}

Custom templates

You can override the default types and templates for your own, setting in the firebase config object a templatesPath field with the string to the path.

{
  "firebase": {
    "cert": "/route/to/firebase-adminsdk.json",
    "url" : "https://your-project.firebaseio.com",
    "templatesPath": "/path/to/templates"
  }
}

You can find examples of the templates format under the folder 'exampleTemplates'.

Event listeners

node-fcm-hero has a default events pre configured to help you to subscribe to listeners depending of this default events.

This is the list of the default events:

push.events :

.UNINSTALLED      
.RENEW_CREDENTIALS
.FIREBASE_ERROR   
.ERROR            
.SUCCESS          

To subscribe and catch some of this events, you need to add a callback funtion asociated to the event:

push.on(push.events.UNINSTALLED, callback1);
push.on(push.events.ERROR, callback2);
push.on(push.events.RENEW_CREDENTIALS, callback3);

UNINSTALLED event

Will return every single token marked as app uninstalled after the called send function.

Error handling

If any error happends, the node-fcm-hero will return a json object with the success field equal to 0. Also, it will notify to the on error listeners.

Test

To make the integration test work, you need to provide your own instance of firebase admin messaging or set your configuration file with the route to your firebase-adminsdk.json. Also you need to you provide at least one working firebase token and one uninstalled to cover all the best scenarios. After that, delete the .skip of the nodeFcmHero.e2e.test.js

Sorry for this, but i don't want to see how my phone ๐Ÿ’ฅ๐Ÿ’ฅ๐Ÿ’ฅ

Contributing

Please read CONTRIBUTING.md for details on code of conduct, and the process for submitting pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

node-fcm-hero's People

Contributors

cluny85 avatar

Watchers

 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.