The bundle provides a public Web Push service that you can inject this service into your application components.
In the following example, let's imagine that a notification is dispatched using the Symfony Messanger component and catched by an event handler. This handler will fetch all subscriptions and send the notification.
The SubscriptionRepository class is totally fictive
src/MessageHandler/SendNotification.php
<?phpdeclare(strict_types=1);namespaceApp\MessageHandler;useApp\Message\SubscriptionExpired;useSymfony\Component\Messenger\Handler\MessageHandlerInterface;useSymfony\Component\Messenger\MessageBusInterface;useWebPush\Notification;useWebPush\WebPush;finalclassSendPushNotificationsimplementsMessageHandlerInterface{privateMessageBusInterface $messageBus;privateSubscriptionRepository $repository;privateWebPush $webPush;publicfunction__construct(MessageBusInterface $messageBus,SubscriptionRepository $repository,WebPush $webPush) {$this->messageBus = $messageBus;$this->repository = $repository;$this->webPush = $webPush; }publicfunction__invoke(Notification $notification):void {// Fetch all subscriptions $subscriptions =$this->repository->fetchAllSubscriptions();foreach ($subscriptions as $subscription) {//Sends the notification to the subscriber $report =$this->webPush->send($notification, $subscription);//If the subscription expiredif ($report->subscriptionExpired()) {//We dispatch a new message and expect for// the subscription to be deleted$this->messageBus->dispatch(newSubscriptionExpired($subscription)); } } }}