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

tiny-push

v2.1.0

Published

Delivering push notifications made simple and straightforward

Downloads

17

Readme

TinyPush

TinyPush is a NodeJS module, providing simple access to push notifications delivery and feedback management


Installation

This module requires NodeJS v6 or newer.

To install in your project, run npm install --save tiny-push

Init

To start the push engine, provide the GCM and APN keys:

var push = require('tiny-push');

const keys = {
	fcmKey: "__YOUR_FCM_KEY_HERE__",
	gcmKey: "__YOUR_GCM_KEY_HERE__",
	apnCertFile: "/path/to/apn.p12",
	apnKeyFile: "/path/to/apn-key.p12",  // both may be in the same file
	production: true                     // false will use the sandbox mode
};

push.init(keys);

The keys argument is required.

If a FCM key is provided, all Android notifications will use FCM. If no FCM key is provided, but a GCM one, then GCM will be used for Android devices.

If a certificate file is provided, APN will be used on iOS devices. If no certificate is provided and an FCM key is defined, then the delivery will be attempted through FCM.

To override the default values, call the init function as follows:

var push = require('tiny-push');

const keys = {
	fcmKey: "__YOUR_FCM_KEY_HERE__",
	gcmKey: "__YOUR_GCM_KEY_HERE__",
	apnCertFile: "/path/to/apn.p12",
	apnKeyFile: "/path/to/apn-key.p12",  // both may be in the same file
	production: true                     // false will use the sandbox mode
};
const defaults = {
	concurrency: 100,             // max simultaneous connections
	
	// Android only
	appName: 'My Application',    // Used as title
	retryCount: 8,                // Retries before ignoring
	delayWhileIdle: false,        // Notify when the phone wakes
	checkPayloadSize: false,      // Throws an error if true and size > 2048
	simulate: false,              // Request without sending
	androidSound: 'default',

	// iOS only
	timeToLive: 60 * 60 * 24 * 2, // 48h
	iosSound: 'default'
};

push.init(keys, defaults);

The defaults parameter is optional. The values above are already the default ones.

Simple use case

If you just want to deliver some notification to a group of users:

let recipients = [
{
	token: '__REGISTRATION_TOKEN_HERE__', 
	platform: 'ios', 
	unread: 2        // iOS Badge
}, {
	token: pushToken, 
	platform: 'android'
}];

Then you can use the main batch method:

const message = "Hi from TinyPush";
const payload = { some: "value" };  // optional

push.batch(recipients, message, payload)
.then(res => {
	console.log(res); // would log: [ undefined, { successful: 1, failed: 0 } ]
})
.catch(err => {
	console.error(err); // Unable to connect
})

Android messages provide immediate results, whereas APN doesn't. However, both allow us to use the Feedback service. (See below).

Platform specific

If you need to customize the notifications depending on the platform or have a large amount of requests, you may have to use the send function for Android and iOS:

Android

const tokens = ["registration_token_1", "registration_token_2", ...];
const message = "Hi from TinyPush";
const payload = { some: "value" };
const androidSound: 'id_launch';

push.fcm.send(tokens, message, payload, androidSound)
.then(res => {
	console.log(res); // would log: [ { successful: 1, failed: 0 } ]
})
.catch(err => {
	console.error(err); // Unable to connect
})

iOS


const tokens = ["registration_token_1", "registration_token_2", ...];
const badges = [2, 3, ...];
const message = "Hi from TinyPush";
const payload = { some: "value" };
const iosSound: 'ic_launcher';
const timeToLive = 60 * 60 * 24; // 1 day

push.apn.send(tokens, message, payload, badges, iosSound, timeToLive)
.then(() => {
	// Going here means that nothing went wrong
})
.catch(err => {
	console.error(err); // Unable to connect
})

The numbers in the badges array have a 1:1 correspondance with the tokens array. If badges[10] equals 2, this means that the phone with the token tokens[10] will receive a notification with a badge of 2.

On iPhone, if we reach the main .then() block, means that no connection error was encountered. However, this does not mean that all the transactions have completed as expected.

Feedback

That's why TinyPush provides a simple way to be notified of updated or invalid registration tokens.

In order to get feedback, you need to define a callback with the followins signature:

function gotFeedback(tokensToUpdate, tokensToRemove){

	// Updated tokens. Android only
	// On iOS tokensToUpdate = []
	tokensToUpdate.forEach(entry => { // token is an object
	
		console.log("FROM", entry.from);
		console.log("TO", entry.to);
		
		// Update here your database
	});
	
	// Invalid tokens
	tokensToRemove.forEach(token => { // token is a string
		
		console.log("REMOVE", token);
		
		// Remove here from the database
	});
}

Now you can register your function as a callback:

push.gcm.onFeedback(gotFeedback);
push.apn.onFeedback(gotFeedback);

NOTE: Apple may eventually give some false positives of tokens to remove. You may want to double check a user's registration token before you decide to remove it from the database.

Credits

Jordi Moraleda

We Are Tvrbo