Web Push Notifications
The Web Push Notifications component integrates with the Web Push Bundle to enable push notifications in your Progressive Web App. This Stimulus controller manages push notification subscriptions and handles user permission requests.
Introduction
Web Push Notifications allow you to send timely, relevant updates to users even when they're not actively using your application. The component provides a simple interface to:
Subscribe users to push notifications
Manage user permission states (granted, denied, default)
Handle subscription lifecycle events
Integrate with the Web Push Bundle for server-side notification delivery
This component requires the Web Push Bundle to be installed and configured. The bundle handles server-side notification delivery using VAPID authentication.
Browser Support
Chrome/Edge
✅ Full
✅ Full
Firefox
✅ Full
✅ Full
Safari
✅ 16.1+
✅ 16.4+
Opera
✅ Full
✅ Full
Samsung Internet
N/A
✅ Full
Safari on macOS requires version 16.1+ and Safari on iOS requires version 16.4+. Web Push is not available in Safari Private Browsing mode.
Prerequisites
Before using Web Push Notifications, you need to:
Install and configure the Web Push Bundle:
Configure VAPID keys in your
config/packages/web_push.yaml:
Generate VAPID keys using the Web Push Bundle command:
Enable Service Worker in your PWA configuration as Web Push requires an active service worker.
Usage
Basic Setup with Twig Live Component
To implement Web Push, you should create either a Stimulus Controller to intercept events or a Twig Live Component. Below is an example of the latter. Please note that you must hook into the Service Worker to show the notifications to the user and allow action interaction
Let say we send the following message. This is a structured notification with buttons 'action1' and 'action2' .
You can decide what to do when the user clicks on the button or the notification. In the example, we will use the data associated to the notification to decide where to redirect the user for action1 , action2 or a click on the notification itself.
The event is of type NotificationEvent and you can access the notification object. Read this page to know more.
As it can become difficult to manage several action names, there is a wildcard action '*' that can be declared. It is not recommended to declare any other handlers when the wildcard is set.
Best Practices
Permission Request Timing
Don't request notification permission immediately when the user visits your site. Instead:
Handle Permission States
Always handle all three permission states:
Default: User hasn't decided yet
Granted: User allowed notifications
Denied: User blocked notifications
Store Subscriptions Securely
Store subscription data securely and associate it with authenticated users:
Notification Delivery
When sending notifications from the server, use the Web Push service:
Common Use Cases
1. News and Content Updates
Notify users when new content is published:
2. Real-time Messaging
Send instant message notifications:
3. E-commerce Order Updates
Keep customers informed about their orders:
4. Reminder System
Send time-based reminders to users:
API Reference
Values
applicationServerKey
applicationServerKeyType: String (required)
The VAPID public key for authentication. This is your VAPID_PUBLIC_KEY from the Web Push Bundle configuration.
Actions
status()
status()Checks the current push notification subscription status and dispatches an event with the result.
Dispatches: pwa--web-push:subscribed or pwa--web-push:unsubscribed
subscribe()
subscribe()Requests notification permission from the user and subscribes to push notifications if granted.
Dispatches:
pwa--web-push:subscribedon successpwa--web-push:deniedif user denies permissionpwa--web-push:erroron error
unsubscribe()
unsubscribe()Unsubscribes the current device from push notifications.
Dispatches: pwa--web-push:unsubscribed
Events
pwa--web-push:subscribed
pwa--web-push:subscribedFired when a user successfully subscribes to push notifications or when checking status finds an active subscription.
Event Details:
Example:
pwa--web-push:unsubscribed
pwa--web-push:unsubscribedFired when a user unsubscribes or when checking status finds no active subscription.
Example:
pwa--web-push:denied
pwa--web-push:deniedFired when the user denies notification permission.
Example:
pwa--web-push:error
pwa--web-push:errorFired when an error occurs during subscription or unsubscription.
Event Details:
Example:
Related Components
Service Worker - Required for Web Push functionality
Web Push Bundle Documentation - Server-side notification delivery
Additional Resources
Last updated
Was this helpful?