Web Push Notifications
If you are interested in Web Push notifications, please read about the bundle we maintain: https://web-push.spomky-labs.com/. This bundle provides a Stimulus Controller to ease the push notification subscription and clicks on the message or buttons (if any).
To implement this, you must create either a Stimulus Controller to intercept events or a Twig Live Component. Below is an example of the latter. Please note that you shall hook into the Service Worker to shwo the notifications to the user and allow action interaction
<?php
namespace App\Twig\Component;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use WebPush\Subscription;
#[AsLiveComponent('WebPush')]
class WebPush
{
use DefaultActionTrait;
use ComponentToolsTrait;
public function __construct(
private readonly \WebPush\WebPush $webpushService
) {
}
#[LiveProp]
public string $status = 'unknown';
/**
* @param array{auth: string, p256dh: string} $keys
* @param string[] $supportedContentEncodings
*/
#[LiveListener('subscribed')]
public function onSubscription(
#[LiveArg] string $endpoint,
#[LiveArg] array $keys,
#[LiveArg] array $supportedContentEncodings
): void {
$this->status = 'subscribed';
$subscription = json_encode([
'endpoint' => $endpoint,
'keys' => $keys,
'supportedContentEncodings' => $supportedContentEncodings,
]);
// Store the $subscription (Filesystem, Databse...)
// If the user is logged in, associate this subscription;
// you will be able to send targeted notification to that
}
#[LiveListener('unsubscribed')]
public function onUnsubscription(
): void {
$this->status = 'unsubscribed';
}
}
Service Worker Hooks
The service worker shall be enable when using web push notifications. Please make sure the configuration is set accordingly.
In your service wroker JS file (we consider it is in assets/sw.js in this example), you must at least enable the push notification support. There are two types of notifications:
Simple: only a message. No payload.
Structured: a message, its payload and optionnaly actions
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.
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.
Parameters
applicationServerKey: the public key for the cypher operations. This parameter is mandatory.
Actions
status: Signals the Stimulus Component to verify subscription status and triggers an event.
subscribe: Asks the user to accept or decline web push notification on the current device.
unsubscribe: Unsubscribe the user from future web push notification on the current device.
Targets
None
Events
pwa--web-push:unsubscribed
pwa--web-push:subscribed
pwa--web-push:denied
pwa--web-push:error
Last updated
Was this helpful?