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

kayako-web-push

v1.0.1

Published

A general purpose library to subscribe user for web push notifications.

Downloads

16

Readme

= Web Push

Web push is a standard protocol to add support for Push notifications with consistent API across different browsers.

Unfortunately, only Chrome and Firefox supports it for now and for Safari we are forced to use APNS which is not a web push standard.

== How it works? Web push starts by registering a service worker in chrome and firefox, which runs in background even when browser tab for your website is closed. This gives extreme power for your website to push notifications to the user and make them act upon it.

NOTE: Do not abuse push notifications. If a user will block your website, it's almost impossible to ask them to re-enable notifications.

Once you are done registering a service worker, you make use of that subscription with a public key to generate a URL and some keys, which are then sent to your server, which is used to push notifications to registered devices.

== Things to take care of?

  1. Make sure it's easier to update service worker file. Service worker doesn't follow the HTTP caching specs and instead checks the file byte by byte to see if it has new contents.

Also this check is performed once in every 24hours and also when no content is found, it's activation is deferred until all connected clients are disconnected. This behavior confuses a lot of people.

== Client API This library exposes a total of 4 methods which we used in sequence in order to register a user for push notifications.

==== hasSupport Checks if browser has support for service workers and push notification.

[source, js]

import { hasSupport } from 'kayako-web-push'

if (hasSupport()) { }

==== getPermission Get the permission status for push notifications, also this method will prompt the user if they never granted or denied the notificiation.

[source, js]

import { getPermission, kwConstants } from 'kayako-web-push'

getPermission() .then((result) => { if (result === kwConstants.GRANTED) { // all good }

if (result === kwConstants.IGNORED) { // user ignored the notification popup }

if (result === kwConstants.DENIED) { // user denied for notifications } })

==== registerServiceWorker(pathToFile, scope) Register the service to be for used for making push notifications. Below is the list of service worker events.

image:https://mdn.mozillademos.org/files/12632/sw-events.png[]

[source, js]

import { registerServiceWorker } from 'kayako-web-push'

registerServiceWorker('/assets/service-worker.js') .then((registeration) => { console.log('registeration object %j', registeration) }).catch((error) => { console.log('something blowed up in service worker file') })

==== registerDevice(registeration, publicKey) The user device with service worker registeration object and the web push compatible public key. You can find sample key link:https://web-push-codelab.appspot.com/[here].

[source, js]

import { registerServiceWorker, registerDevice } from 'kayako-web-push'

registerServiceWorker('/assets/service-worker.js') .then(registerDevice) .then((payload) => { console.log('registeration payload is %j', payload.toJSON()) }) .catch((error) => { console.log('something blowed up') })

== Server API's Below is the list of available server API's to be used.

==== Getting Public API We need to make use of the /api/v1/credentials to get the public API.

==== User Registeration POST - api/v1/devices with below payload.

[source, json]

{ "device_type": "Chrome", "fingerprint_uuid": "UNIQUE_PER_USER_PER_DEVICE", "device_properties": { "native_device_id": "NA", "device_token": "NA (confirmed)", "last_ip_address": "192.168.1.12", "subscription_info": "{"endpoint" : "http://localhost/push", "keys": {"auth" : "4nJEYUSCf0YGm1jK", "p256dh": " blZVP7KqrJd1lgwb"}}", "os_version": "Mac", "device_manufacturer": "Apple", "device_model": "MacbookPro", "screen_size": "1280x768" } }

== Permission Types

  1. default - The guy skipped the notification prompt.
  2. denied - The guy denied it.
  3. granted - All good

== Service Worker Lifecycle image:https://mdn.mozillademos.org/files/12636/sw-lifecycle.png[]

== Browser Support http://caniuse.com/#feat=push-api