Web Push Notifications

The Web Push Notifications component integrates with the Web Push Bundlearrow-up-right 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

circle-info

This component requires the Web Push Bundlearrow-up-right to be installed and configured. The bundle handles server-side notification delivery using VAPID authentication.

Browser Support

Browser
Desktop
Mobile

Chrome/Edge

✅ Full

✅ Full

Firefox

✅ Full

✅ Full

Safari

✅ 16.1+

✅ 16.4+

Opera

✅ Full

✅ Full

Samsung Internet

N/A

✅ Full

circle-exclamation

Prerequisites

Before using Web Push Notifications, you need to:

  1. Install and configure the Web Push Bundle:

  1. Configure VAPID keys in your config/packages/web_push.yaml:

  1. Generate VAPID keys using the Web Push Bundle command:

  1. 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.

circle-info

The event is of type NotificationEventarrow-up-right 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:

  1. Default: User hasn't decided yet

  2. Granted: User allowed notifications

  3. 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

Type: String (required)

The VAPID public key for authentication. This is your VAPID_PUBLIC_KEY from the Web Push Bundle configuration.

Actions

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()

Requests notification permission from the user and subscribes to push notifications if granted.

Dispatches:

  • pwa--web-push:subscribed on success

  • pwa--web-push:denied if user denies permission

  • pwa--web-push:error on error

unsubscribe()

Unsubscribes the current device from push notifications.

Dispatches: pwa--web-push:unsubscribed

Events

pwa--web-push:subscribed

Fired when a user successfully subscribes to push notifications or when checking status finds an active subscription.

Event Details:

Example:

pwa--web-push:unsubscribed

Fired when a user unsubscribes or when checking status finds no active subscription.

Example:

pwa--web-push:denied

Fired when the user denies notification permission.

Example:

pwa--web-push:error

Fired when an error occurs during subscription or unsubscription.

Event Details:

Example:

Additional Resources

Last updated

Was this helpful?