To add web push notifications to a React website, put a service worker file in your public folder, register it when the app loads, and show a “Turn on notifications” button. On click, request permission and call pushManager.subscribe with your VAPID public key. Then send the subscription to your server, which sends the messages.
React website web push follows the same browser standards as any other site. React only changes where the code lives: the service worker is a static file, and the permission flow becomes a small component. Below I show a working setup for a Vite React app, the common mistakes, and when a push platform saves you work. If you use Next.js, read my Next.js web push guide instead.
What do you need before adding web push to React?
You need HTTPS, a service worker served from your own domain, a VAPID key pair and a backend that stores subscriptions and sends messages. React itself needs no extra package.
| Piece | Where it lives in a React app | Purpose |
|---|---|---|
| HTTPS | Your hosting (localhost is fine for testing) | Push needs a secure context |
| Service worker | public/sw.js, served at /sw.js | Receives pushes and shows notifications |
| VAPID public key | A build-time env variable, e.g. VITE_VAPID_PUBLIC_KEY | Passed to pushManager.subscribe |
| Subscribe component | A React component with a button | Asks permission and creates the subscription |
| Backend sender | Node.js, Python, PHP or a push panel | Stores subscriptions and sends messages |
Only the public key goes into React code. In Vite, variables prefixed with VITE_ are bundled into the client, so the private key must never get that prefix. My post on VAPID keys for web push explains the pair.
Where does the service worker go in a React app?
Put sw.js in the public folder. Vite copies files in public/ to the root of your build unchanged, as its docs on the public directory describe, so the file is served at /sw.js on your own origin.
That location matters for two reasons. First, browsers only accept a service worker from the same origin as the page. Second, its default scope is the folder it’s served from, so the root covers every route in your single-page app. Don’t import the service worker into your bundle; it must stay a separate file.
// public/sw.js
self.addEventListener('push', (event) => {
const data = event.data ? event.data.json() : {};
event.waitUntil(
self.registration.showNotification(data.title || 'New update', {
body: data.body,
icon: '/icon-192.png',
data: { url: data.url || '/' }
})
);
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
event.waitUntil(clients.openWindow(event.notification.data.url));
});If your app already uses a PWA plugin that generates its own service worker, add the push listeners to that worker instead of registering a second one at the same scope. My guide to the service worker for web push covers scope and updates.
How do you build the subscribe button in React?
Build a component that checks support, registers the service worker and subscribes only when the visitor clicks. Here’s a compact version.
import { useEffect, useState } from 'react';
const PUBLIC_KEY = import.meta.env.VITE_VAPID_PUBLIC_KEY;
function urlBase64ToUint8Array(base64) {
const padding = '='.repeat((4 - (base64.length % 4)) % 4);
const raw = atob((base64 + padding).replace(/-/g, '+').replace(/_/g, '/'));
return Uint8Array.from([...raw].map((c) => c.charCodeAt(0)));
}
export default function PushButton() {
const [status, setStatus] = useState('checking');
useEffect(() => {
if (!('serviceWorker' in navigator) || !('PushManager' in window)) {
setStatus('unsupported');
return;
}
navigator.serviceWorker.register('/sw.js')
.then((reg) => reg.pushManager.getSubscription())
.then((sub) => setStatus(sub ? 'subscribed' : 'idle'));
}, []);
async function subscribe() {
const permission = await Notification.requestPermission();
if (permission !== 'granted') { setStatus('denied'); return; }
const reg = await navigator.serviceWorker.ready;
const sub = await reg.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(PUBLIC_KEY)
});
await fetch('/api/subscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(sub)
});
setStatus('subscribed');
}
if (status === 'unsupported' || status === 'checking') return null;
if (status === 'subscribed') return <p>Notifications are on.</p>;
if (status === 'denied') return <p>Notifications are blocked in your browser settings.</p>;
return <button onClick={subscribe}>Turn on notifications</button>;
}Registering the worker in useEffect is fine, because registration shows nothing to the visitor. The permission request, however, sits in the click handler. MDN’s PushManager.subscribe() reference lists every option.
Why should permission be requested on click?
Ask on a click because browsers increasingly ignore or quiet prompts that appear without a user action, and Safari on iPhone only shows the prompt in response to a tap. So calling Notification.requestPermission in useEffect on page load fails on iPhone and annoys everyone else.
A visible button also lets you explain the value first, such as “Get price-drop alerts”. For copy and timing ideas, see my notes on push permission prompt best practices.
How do you send the notifications?
Your backend sends them, not React. Store each subscription it receives, then send with a web push library that handles VAPID signing and encryption. My guides cover sending from Node.js, sending from Python and sending from PHP or Laravel.
Whatever language you use, delete subscriptions that return 404 or 410. Those codes mean the visitor unsubscribed or the subscription expired.
Does React website web push work on iPhone?
Yes, on iOS 16.4 and later, but only after the visitor adds your site to the Home Screen. You need a web app manifest with display set to standalone, as WebKit’s web push announcement explains. A small banner that tells iPhone users how to add the site helps here. The full checklist is in how to get web push working on iPhone.
Should you build it yourself or use a push platform?
Build it yourself if you need a few event-driven alerts and have developers to run the backend. Use a platform if marketing or content teams will send campaigns.
| Need | Build it yourself | Push platform |
|---|---|---|
| Subscribe button in React | Your component | Platform script or your component |
| Sending code and key storage | Your backend | Included |
| Campaign editor and scheduling | You build it | Included |
| Segments and click reports | You build it | Included |
With a platform, the React work shrinks to adding its script and placing its service worker file in public/. For more on the options, see how to add push notifications to a website.
Where does LaraPush fit?
LaraPush is a self-hosted web push panel that works on any site that serves JavaScript, including React apps. You add its script and service worker, and it handles subscriptions, campaigns, scheduling, segmentation and reports. Its REST API has no call limits, so your app’s backend can trigger sends too. See the features overview and the setup guide.
The trade-offs: you run your own VPS, the licence covers one server, and there’s no free plan. Plans start from $499 one-time; check the pricing page for current prices. My roundup of push notification services covers other options.
Frequently asked questions
No. The Push API, Notifications API and service workers are browser features, so plain React code is enough. A small helper to convert the VAPID key and a button component cover the client side. The sending side needs a web push library on your server.
Yes. Create React App also serves files from the public folder at the site root, so the same sw.js approach works. The React team deprecated Create React App in 2025, though, so new projects usually start with Vite or a framework.
On iPhone, web push only works in a site added to the Home Screen on iOS 16.4 or later. In a normal Safari tab, PushManager isn’t available, so a support check hides the button. Add a manifest and show install instructions.
Yes. Browsers treat localhost as a secure context, so service workers and push subscriptions work without a certificate. For iPhone testing, deploy to a real HTTPS domain and add the site to the Home Screen.
Yes, registering in useEffect after the first render is fine, because it doesn’t show anything to the visitor. Only the permission request must wait for a click. Register once, at the app root, rather than in many components.




