Web Push API
The API for subscribing/unsubscribing to Push API notifications and optional syncing the subscription to your backend.
Installation
npm i --save web-push-api
Usage
Abstract example.
; if isSupported ;
React component
;;; ; const flow = !isSupported ? null : ; /** * @param */ { ;} /** * @param {function(state: Object): void} updateState * @param {('getPermission'|'getSubscription'|'subscribe'|'unsubscribe')} method * @param {('permission'|'subscription')} affects * @param * @param */ { async { let value = valueOnError; try value = await flowmethodapplicationServerKey; catch error ; ; };} /** * @param {function(state: Object): void} updateState * @param {('subscribe'|'unsubscribe')} method * @param * * @return {function(event: Event): void} */ { return { event; ; ; };} { const permission subscription processing = true } setState = ; const updateState = ; let children; ; if processing children = <Spinner small /> ; else if permission !== 'granted' || offline const title = offline ? 'This feature is not available since the app is offline. Please come back later.' : 'Please turn notifications on. This will allow you receiving updates even if the application is closed.'; children = <span title= title > <i className="icon-notifications-off" /> </span> ; else if subscription === null children = <button title="Click to subscribe to push notifications." onClick= > <i className="icon-notifications-none" /> </button> ; else children = <button title="Click to unsubscribe from push notifications." onClick= > <i className="icon-notifications-active" /> </button> ; return <div className="push-notifications"> children </div> ;} PushNotificationsSubscriberpropTypes = offline: PropTypesboolisRequired applicationServerKey: PropTypesstringisRequired; ? PushNotificationsSubscriber : null;