kayako-web-push
v1.0.1
Published
A general purpose library to subscribe user for web push notifications.
Downloads
143
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?
- 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
- default - The guy skipped the notification prompt.
- denied - The guy denied it.
- granted - All good
== Service Worker Lifecycle image:https://mdn.mozillademos.org/files/12636/sw-lifecycle.png[]
== Browser Support http://caniuse.com/#feat=push-api