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

push-notification-node

v1.0.7

Published

push notification for FCM and APN

Downloads

16

Readme

push-notification-node NPM version

Android and IOS Push notification by FCM APN. A Node.JS simple interface to Apple Push Notification (APN) and Google's Firebase Cloud Messaging (FCM). Supports both android and iOS

Installation

$ npm install push-notification-node

Usage

There are 2 ways to use this lib:

The Promise 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 sendPromise() function

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

Promise usage example:

    const { FCM } = require('push-notification-node');

    const GOOGLE_KEY = 'YOURSERVERKEYHERE'; //put your server key here
    const token = 'DEVICETOKEN'; // device token from the app provide by the fcm

    const data = { //this may vary according to the message type (single recipient, multicast, etc)
        body: 'hello world',
        title: 'your message',
        notificationCode: 1,
         data: {  //you can send only notification or only data(or include both)
            my_key: 'my value',
            my_another_key: 'my another value'
        }
    }; // message object

    const fcm = new FCM(GOOGLE_KEY); // set the google key

    fcm.sendPromise(token, data).then((res) => {
        console.log(res);
    }).catch((err) => {
        console.log(err);
    });

Callback usage example:

    const fcm = new FCM(GOOGLE_KEY); // set the google key

   fcm.sendPush(token, data, function(err, res) {
        if (err) {
            console.log(err);
        } else {
            console.log(res);
        }
    });

Send to multiple tokens

 const { FCM } = require('push-notification-node');

    const GOOGLE_KEY = 'YOURSERVERKEYHERE'; //put your server key here
    const token = ['DEVICETOKEN', 'DEVICETOKEN']; // device token from the app provide by the fcm

    const data = { //this may vary according to the message type (single recipient, multicast, etc)
        body: 'hello world',
        title: 'your message',
        notificationCode: 1,
         data: {  //you can send only notification or only data(or include both)
            my_key: 'my value',
            my_another_key: 'my another value'
        }
    }; // message object
    const fcm = new FCM(GOOGLE_KEY); // set the google key

    fcm.sendPromise(token, data).then((res) => {
        console.log(res);
    }).catch((err) => {
        console.log(err);
    });

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)

const message = {
  data: { android: {
            ttl: 3600,
            notification: {
            title: '---',
            body: '----',
            icon: 'icon',
            color: '#f45342'
            }
        }
  },
   priority: 'high',
  topic: 'name'
};

WebPush-specific fields

const message = {
  webpush: {
    notification: {
      title: '---',
      body: '---',
      icon: 'https://my-server/icon.png'
    }
  },
   priority: 'normal',
  topic: 'name'
};

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:

const message = {
  notification: {
    title: '---',
    body: '---',
  },
  android: {
    ttl: 3600 * 1000,
    notification: {
      icon: '---',
      color: '#d45f42',
    },
  },
  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