Web Push Notifications
<?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
Parameters
Actions
Targets
Events
Last updated
Was this helpful?