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

pushpad

v1.0.0

Published

Web push notifications for Chrome, Firefox and Safari using Pushpad.

Downloads

4,399

Readme

Pushpad - Web Push Notifications

Build Status

Pushpad is a service for sending push notifications from your web app. It supports the Push API (Chrome, Firefox, Opera, Edge) and APNs (Safari desktop). iOS support is coming in 2023.

Features:

  • notifications are delivered even when the user is not on your website
  • users don't need to install any app or plugin
  • you can target specific users or send bulk notifications

Installation

Using NPM:

npm install pushpad

Getting started

First you need to sign up to Pushpad and create a project there.

Then set your authentication credentials and project:

var pushpad = require('pushpad');

var project = new pushpad.Pushpad({
  authToken: AUTH_TOKEN,
  projectId: PROJECT_ID
});
  • authToken can be found in the user account settings.
  • projectId can be found in the project settings.

Collecting user subscriptions to push notifications

You can subscribe the users to your notifications using the Javascript SDK, as described in the getting started guide.

If you need to generate the HMAC signature for the uid you can use this helper:

project.signatureFor(currentUserId)

Sending push notifications

var pushpad = require('./index');

var AUTH_TOKEN = 'e991832a51afc9da49baf29e7f9de6a6';
var PROJECT_ID = 763;

var project = new pushpad.Pushpad({
  authToken: AUTH_TOKEN,
  projectId: PROJECT_ID
});

var notification = new pushpad.Notification({
  project: project,
  body: 'Hello world!',
  title: 'Website Name', // optional, defaults to your project name
  targetUrl: 'https://example.com', // optional, defaults to your project website
  iconUrl: 'https://example.com/assets/icon.png', // optional, defaults to the project icon
  badgeUrl: 'https://example.com/assets/badge.png', // optional, defaults to the project badge
  imageUrl: 'https://example.com/assets/image.png', // optional, an image to display in the notification content
  ttl: 604800, // optional, drop the notification after this number of seconds if a device is offline
  requireInteraction: true, // optional, prevent Chrome on desktop from automatically closing the notification after a few seconds
  silent: false, // optional, enable this option if you want a mute notification without any sound
  urgent: false, // optional, enable this option only for time-sensitive alerts (e.g. incoming phone call)
  customData: '123', // optional, a string that is passed as an argument to action button callbacks
  // optional, add some action buttons to the notification
  // see https://pushpad.xyz/docs/action_buttons
  actions: [
    {
      title: 'My Button 1',
      targetUrl: 'https://example.com/button-link', // optional
      icon: 'https://example.com/assets/button-icon.png', // optional
      action: 'myActionName' // optional
    }
  ],
  starred: true, // optional, bookmark the notification in the Pushpad dashboard (e.g. to highlight manual notifications)
  // optional, use this option only if you need to create scheduled notifications (max 5 days)
  // see https://pushpad.xyz/docs/schedule_notifications
  sendAt: new Date(Date.UTC(2016, 7 - 1, 25, 10, 9)), // 2016-07-25 10:09 UTC
  // optional, add the notification to custom categories for stats aggregation
  // see https://pushpad.xyz/docs/monitoring
  customMetrics: ['examples', 'another_metric'] // up to 3 metrics per notification
});

// deliver to a user
notification.deliverTo(user1, function(err, result) { /*...*/ });

// deliver to a group of users
notification.deliverTo([user1, user2, user3], function(err, result) { /*...*/ });

// deliver to some users only if they have a given preference
// e.g. only "users" who have a interested in "events" will be reached
notification.deliverTo(users, { tags: ['events'] }, function (err, result) { /*...*/ });

// deliver to segments
// e.g. any subscriber that has the tag "segment1" OR "segment2"
notification.broadcast({ tags: ['segment1', 'segment2'] }, function (err, result) { /*...*/ });

// you can use boolean expressions 
// they must be in the disjunctive normal form (without parenthesis)
var filter1 = ['zip_code:28865 && !optout:local_events || friend_of:Organizer123'];
notification.broadcast({ tags: filter1 }, function (err, result) { /*...*/ });
var filter2 = ['tag1 && tag2', 'tag3']; //  equal to 'tag1 && tag2 || tag3'
notification.deliverTo(users, { tags: filter2 }, function (err, result) { /*...*/ });

// deliver to everyone
notification.broadcast(function(err, result) { /*...*/ });

You can set the default values for most fields in the project settings. See also the docs for more information about notification fields.

If you try to send a notification to a user ID, but that user is not subscribed, that ID is simply ignored.

The methods above return an object:

  • id is the id of the notification on Pushpad
  • scheduled is the estimated reach of the notification (i.e. the number of devices to which the notification will be sent, which can be different from the number of users, since a user may receive notifications on multiple devices)
  • uids (deliverTo only) are the user IDs that will be actually reached by the notification because they are subscribed to your notifications. For example if you send a notification to ['uid1', 'uid2', 'uid3'], but only 'uid1' is subscribed, you will get ['uid1'] in response. Note that if a user has unsubscribed after the last notification sent to him, he may still be reported for one time as subscribed (this is due to the way the W3C Push API works).
  • send_at is present only for scheduled notifications. The fields scheduled and uids are not available in this case.

License

The library is available as open source under the terms of the MIT License.