Skip to content
ShipFastDev
All posts

Article

Web push notifications in Laravel and React: service worker, VAPID and an inbox

, 8 min read

To add web push notifications to a Laravel + React app, you need four pieces: a service worker that receives push messages and displays them, a VAPID key pair so push services can identify your server, a table of browser subscriptions per user, and a queued job that sends the message when something happens. Store a notification history alongside it, because a push is easy to miss and some visitors can never receive one. Here is how that was built on Driveway Morocco, ShipFastDev's car-rental platform, and the bugs found before it worked reliably.

The pieces, in the order a message travels

Push is two technologies. web.dev's push notifications overview separates them: push sends messages from your server to users "even when they're not actively using your website", and notifications display that information on the device. In between sits a push service run by the browser vendor, which receives your request and delivers it, queuing it if the device is offline.

In a Laravel + React app the path looks like this:

  1. A domain event fires in Laravel (on Driveway Morocco: a booking is confirmed, cancelled, checked out or returned).
  2. A queued listener writes a row to a notifications table, then sends the push.
  3. The PHP library signs the request with your VAPID private key and posts it to the subscription endpoint.
  4. The browser's push service wakes your service worker, which shows the notification.
  5. A click opens the right page, and the in-app inbox shows the same message with its read state.

Start from the event, not from the push

On Driveway Morocco, one listener subscribes to the four booking events. Its first step is to save the notification (user, booking, type, title, body, a JSON data field and read_at). Its second step is to send the push. Saving first has two benefits. The inbox becomes the record of what each customer was told, whether or not the push arrived. And it works for guest bookings too: the table has a guest_email column next to user_id, so someone who booked without an account still has a history to look up.

The browser side: service worker, VAPID and subscriptions

MDN's Push API page is explicit: "For an app to receive push messages, it has to have an active service worker." The service worker subscribes through PushManager.subscribe(), and the resulting subscription contains an endpoint and the encryption keys your server needs. Treat that endpoint like a password. MDN calls it "a unique capability URL" and says it "needs to be kept secret", because knowing it is enough to send messages to that browser.

Two options of subscribe() matter:

  • userVisibleOnly: true: a promise that every push shows something to the user. Chrome and Edge reject the subscription if it isn't set to true.
  • applicationServerKey: your VAPID public key, an ECDSA P-256 key. Once it is set, every message from your server must carry a JWT signed with the matching private key.

VAPID itself is defined in RFC 8292 (November 2017): a way for an application server to "voluntarily identify itself to a push service", signing with ES256 on the P-256 curve. Generate the pair once; only the public key goes to the front end.

On the Laravel side, Driveway Morocco uses minishlink/web-push (version 11 in composer.json). Its README lists the requirements (PHP 8.2+ with the mbstring, curl and openssl extensions, with bcmath or gmp recommended for speed) and the methods you will use: VAPID::createVapidKeys() to generate keys, queueNotification() and flush() to send, and isSubscriptionExpired() on each report so you can delete subscriptions that no longer exist. Delete those rows. Otherwise you will retry dead endpoints indefinitely.

The service worker can stay small. This is the shape of the handler (a generic version, not the production file):

// sw.js
self.addEventListener('push', (event) => {
  const data = event.data ? event.data.json() : {};
  event.waitUntil(self.registration.showNotification(data.title, {
    body: data.body, data: { url: data.url }
  }));
});
self.addEventListener('notificationclick', (event) => {
  event.notification.close();
  event.waitUntil(clients.openWindow(event.notification.data.url));
});

MDN's notificationclick reference shows the fuller pattern: close the notification, wrap the work in event.waitUntil(), focus an existing window if one is already on that URL, otherwise call clients.openWindow(). On Driveway Morocco, the click deep-links to the booking the message is about.

Ask for permission when the user wants it

Google's permission UX guidance is blunt: "The worst thing you can do is to show the permission dialog to users as soon as they land on your site." It also warns that once a user blocks the request, "your web app can't ask for permission again". MDN adds that subscribe() calls should follow a user gesture such as a button click.

Driveway Morocco follows the pattern web.dev recommends, which is your own prompt before the browser's:

  • A banner in the public layout ("Activer les notifications", since the storefront defaults to French) that invites visitors to turn notifications on, instead of a browser prompt on page load.
  • A toggle in the customer's profile that subscribes or unsubscribes, so there is always a visible way to turn push off. web.dev says your site should explain how to disable push.

The inbox: one API for web and mobile

A notification bell is the other half of the feature. On Driveway Morocco it is backed by four endpoints: list, unread count, mark one as read, mark all as read. The same controller is registered twice: in the API routes behind Sanctum Bearer tokens for the mobile app, and in the web routes behind the normal session for the React storefront. On the web, the bell shows an unread badge and a dropdown with the 10 most recent items, each one linking to its booking. In the Expo app, the same data opens in a slide-up modal with "mark all as read".

Native mobile push is a separate channel. The Expo app registers a device token, and the backend sends through Expo's push API in batches with a 5-second timeout. Tokens are stored with a platform column, next to the web subscriptions. If you are planning the app side, see the Google Play publishing checklist for Expo apps and the React Native vs Flutter comparison.

What went wrong, and the fixes

1. Notifications were never sent

The listener implemented ShouldQueue, but the Docker Compose setup had no queue worker. A backend audit found that queued emails, push notifications and a scheduled job had all been "silently never executing". The fix was to add worker and scheduler services. The Laravel queue documentation explains why this matters in production: queue:work is a long-lived process, so "you should use a process monitor such as Supervisor" to restart it, and workers must be restarted on each deploy because they keep the booted application in memory.

2. Any user could mark any notification as read

The same audit found that the mark-read endpoint updated a notification by ID without checking who owned it. The fix was a one-line ownership check before the update. Any endpoint that takes a notification ID needs that check.

3. Retries could pile up

Every queued listener and mailable now sets $tries = 3, $backoff = [10, 60, 300] and $maxExceptions = 3, so a provider outage doesn't trigger an endless retry storm. Laravel supports a backoff array for exponential delays (see the queue docs above).

4. Jobs ran before the data existed

A job dispatched inside a database transaction can start before the transaction commits. Driveway Morocco turned on after_commit for its queue connection. According to the Laravel docs, this makes queued listeners, mailables and notifications wait until open transactions have committed.

5. iPhones can't always receive web push

The WebKit announcement for iOS 16.4 says a web app "that has been added to the Home Screen" can request push permission, and only in response to direct user interaction. Most visitors to a rental site never install it. The project's PWA notes made this a rule early on: push is never the only channel. Booking confirmation and status emails still go out, and the inbox shows the same message to anyone who signs in.

A checklist before you ship web push

  • VAPID keys kept in environment variables; expired subscriptions deleted.
  • The notification row saved first, the push sent second, from a queued listener.
  • A supervised queue worker, restarted on deploy, with retries, backoff and dispatch after commit.
  • Permission asked from a button, never on page load, with a visible off switch.
  • An ownership check on every inbox endpoint.
  • Email (or another channel) for anything the customer must not miss.
  • Tests: the push sender mocked in feature tests (Driveway Morocco's listener tests assert the VAPID calls), and a browser test for the bell. The notification-bell Playwright spec runs in CI, as described in Playwright end-to-end tests in CI.

More projects built this way are in the portfolio.

Frequently asked questions

Do web push notifications work on iPhone?

Since iOS 16.4, yes, but only for a web app the user has added to the Home Screen, and the permission request must follow a tap. A Safari visitor who hasn't installed the app can't receive web push, so keep email or an in-app inbox as well.

Do I need Firebase to send web push from Laravel?

Not for browsers. The standard Push API with a VAPID key pair is enough: your server signs each request and the browser's own push service delivers it.

What's the difference between a push notification and an in-app notification?

A push notification is delivered by the operating system or browser, even when your site is closed. An in-app notification is a row in your database, shown in a bell or inbox when the user opens the app. Use push for the alert and the inbox for the record.

Why are my Laravel push notifications not being sent?

The most common cause is that nothing is processing the queue. If the listener implements ShouldQueue, a queue:work process must be running under a process monitor. After that, check for expired subscriptions and for a VAPID key that doesn't match the one the browser subscribed with.

Sources

  1. Push API, MDN Web Docs, 2025-05
  2. PushManager: subscribe() method, MDN Web Docs, 2025-06
  3. ServiceWorkerGlobalScope: notificationclick event, MDN Web Docs, 2026-07
  4. Push notifications overview, web.dev (Google), 2020-11
  5. Permission UX, web.dev (Google), 2025-03
  6. RFC 8292: Voluntary Application Server Identification (VAPID) for Web Push, IETF, 2017-11
  7. Web Push for Web Apps on iOS and iPadOS, WebKit, 2023-02
  8. web-push-php README, web-push-libs (GitHub), 2026-10
  9. Queues (Laravel 13.x), Laravel, 2026-10

Want this kind of work on your product?

Tell Adil what you’re building. You get a reply within 1 business day with times for a 15-minute video call.