# The Web Push Service

The bundle provides a public Web Push service that you can inject into your application components.

In the following example, let's imagine that a notification is dispatched using the Symfony Messenger component and caught by an event handler. This handler will fetch all subscriptions and send the notification.

{% hint style="info" %}
The SubscriptionRepository class is totally fictive
{% endhint %}

{% code title="src/MessageHandler/SendNotification.php" %}

```php
<?php

declare(strict_types=1);

namespace App\MessageHandler;

use App\Message\SubscriptionExpired;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;
use WebPush\Notification;
use WebPush\WebPushService;

#[AsMessageHandler]
final readonly class SendPushNotifications
{
    public function __construct(
        private MessageBusInterface $messageBus,
        private SubscriptionRepository $repository,
        private WebPushService $webPush
    ) {
    }

    public function __invoke(Notification $notification): void
    {
        // Fetch all subscriptions
        $subscriptions = $this->repository->fetchAllSubscriptions();

        // Send to all subscriptions at once
        $reports = $this->webPush->sendToMultiple($notification, $subscriptions);

        // Handle expired subscriptions
        $expired = \WebPush\StatusReport::filterExpired($reports);
        foreach ($expired as $report) {
            // Dispatch a message to delete expired subscription
            $this->messageBus->dispatch(
                new SubscriptionExpired($report->getSubscription())
            );
        }

        // Optionally: handle retryable errors
        $retryable = \WebPush\StatusReport::filterRetryable($reports);
        foreach ($retryable as $report) {
            // Queue for retry (5xx or 429 errors)
            $this->messageBus->dispatch(
                new RetryNotification($report->getNotification(), $report->getSubscription())
            );
        }
    }
}
```

{% endcode %}

## Validation Exceptions

When creating notifications from user input or configuration, validation exceptions provide clear error messages with contextual properties:

{% code title="src/Service/NotificationFactory.php" %}

```php
<?php

declare(strict_types=1);

namespace App\Service;

use Psr\Log\LoggerInterface;
use WebPush\Exception\InvalidTopicException;
use WebPush\Exception\InvalidTTLException;
use WebPush\Exception\ValidationException;
use WebPush\Notification;

final readonly class NotificationFactory
{
    public function __construct(
        private LoggerInterface $logger
    ) {}

    public function createFromRequest(array $data): ?Notification
    {
        try {
            return Notification::create()
                ->withTopic($data['topic'] ?? 'default')
                ->withTTL($data['ttl'] ?? Notification::TTL_ONE_HOUR)
                ->withPayload($data['message'] ?? '');

        } catch (InvalidTopicException $e) {
            $this->logger->error('Invalid topic in request', [
                'topic' => $e->topic,
                'error' => $e->getMessage()
            ]);
            return null;

        } catch (InvalidTTLException $e) {
            $this->logger->error('Invalid TTL in request', [
                'ttl' => $e->ttl,
                'error' => $e->getMessage()
            ]);
            return null;

        } catch (ValidationException $e) {
            $this->logger->error('Validation error', [
                'error' => $e->getMessage()
            ]);
            return null;
        }
    }
}
```

{% endcode %}

See the [Exceptions](/common-concepts/exceptions.md) documentation for complete error handling strategies.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://web-push.spomky-labs.com/the-symfony-bundle/the-web-push-service.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
