JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 22
  • Score
    100M100P100Q65925F
  • License MIT

Push and Notifications API wrapper.

Package Exports

  • push-js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (push-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Push-JS

Table of Contents

Purpose

Wrapper around Push and Notifications API. Handles subscription and unsubscription. Provides some useful methods.

Needs service worker to be installed in any way. Provides an interface to perform service worker installation if needed.

Does not provide other service worker related stuff and service worker itself.

Usage

// ES6
import Push from 'push-js';

// ES5
const Push = require('push-js');

const push = new Push(); // All arguments are optional
push.subscribe().then(subscription => {});
push.unsubscribe().then(() => {});

Constructor parameters

options (optional)

An options object that can contain some callbacks and a path to the service worker.

  • stateChangeCb: receives state and an Error object if an error occurred.
  • subscriptionUpdateCb: receives subscription object (PushSubscription).
  • logCb: receives anything that should be logged.

Instance methods

Push.prototype.subscribe()

Creates and returns new subscription if it does n't exist, returns old otherwise.

Push.prototype.unsubscribe()

Tries to unsubscribe. Returns resolved Promise anyway.

Static methods

Push.getPermissionState()

Returns permission state. See ./source/constants/states.js.

Push.requestPermission()

Requests user permission to show notifications. Returns resolved or rejected Promise depending on user decision.

Push.checkSupport()

Checks browser support and returns Boolean.

Push.getSubscription()

Same as PushManager.getSubscription().

Push.showNotification()

Accepts two parameters, same as new Notification constuctor does. See MDN for the description. Shows notification via service worker. Returns Promise resolved with corresponding Notification object.

Version 1.0.1