
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
web-push-api
Advanced tools
Utility to subscribe/unsubscribe to Push API notifications and syncing with backend.
The API for subscribing/unsubscribing to Push API notifications and optional syncing the subscription to your backend.
npm i --save web-push-api
import { isSupported, getPushSubscriptionFlow, getPushSubscriptionPayload } from 'web-push-api';
if (isSupported) {
getPushSubscriptionFlow((method, pushSubscription) => {
sendRequestToBackend(method, getPushSubscriptionPayload(pushSubscription));
});
}
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { isSupported, getPushSubscriptionFlow, getPushSubscriptionPayload } from 'web-push-api';
import Spinner from 'your-spinner-component';
const flow = !isSupported ? null : getPushSubscriptionFlow((method, pushSubscription) => {
fetch('https://example.com/web-push-api/subscription', { method, body: getPushSubscriptionPayload(pushSubscription) })
.then((response) => response.json())
.then(({ errors }) => errors.map(showError))
.catch(showError);
});
/**
* @param {Error|string} error
*/
function showError(error) {
alert(error instanceof Error ? error.message : error);
}
/**
* @param {function(state: Object): void} updateState
* @param {('getPermission'|'getSubscription'|'subscribe'|'unsubscribe')} method
* @param {('permission'|'subscription')} affects
* @param {string|null} valueOnError
* @param {string} [applicationServerKey]
*/
function doFlowAction(updateState, method, affects, valueOnError, applicationServerKey) {
(async () => {
let value = valueOnError;
try {
value = await flow[method](applicationServerKey);
} catch (error) {
showError(error);
}
updateState({ processing: false, [affects]: value });
})();
}
/**
* @param {function(state: Object): void} updateState
* @param {('subscribe'|'unsubscribe')} method
* @param {string} [applicationServerKey]
*
* @return {function(event: Event): void}
*/
function getFlowActionHandler(updateState, method, applicationServerKey) {
return (event) => {
event.preventDefault();
updateState({ processing: true });
doFlowAction(updateState, method, 'subscription', null, applicationServerKey);
};
}
function PushNotificationsSubscriber({ offline, applicationServerKey }) {
const [{ permission, subscription, processing = true }, setState] = useState({});
const updateState = (state) => setState((prevState) => ({ ...prevState, ...state }));
let children;
useEffect(() => {
if (permission === undefined) {
doFlowAction(updateState, 'getPermission', 'permission', 'denied');
} else if (!offline && permission === 'granted' && subscription === undefined) {
doFlowAction(updateState, 'getSubscription', 'subscription', null);
}
}, [offline, permission, subscription, updateState]);
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={ getFlowActionHandler(updateState, 'subscribe', applicationServerKey) }
>
<i className="icon-notifications-none" />
</button>
);
} else {
children = (
<button
title="Click to unsubscribe from push notifications."
onClick={ getFlowActionHandler(updateState, 'unsubscribe') }
>
<i className="icon-notifications-active" />
</button>
);
}
return (
<div className="push-notifications">
{ children }
</div>
);
}
PushNotificationsSubscriber.propTypes = {
offline: PropTypes.bool.isRequired,
applicationServerKey: PropTypes.string.isRequired,
};
export default flow ? PushNotificationsSubscriber : () => null;
FAQs
Utility to subscribe/unsubscribe to Push API notifications and syncing with backend.
The npm package web-push-api receives a total of 1 weekly downloads. As such, web-push-api popularity was classified as not popular.
We found that web-push-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.