Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Webpush Bundle Laravel Package

bentools/webpush-bundle

Symfony bundle to send Web Push notifications using the Web Push protocol. Manage user-to-subscription associations (multi-device and shared devices) with your own persistence (Doctrine or custom). Includes VAPID key generation and backend APIs for subscriptions.

View on GitHub
Deep Wiki
Context7

Now, send notifications!

Here's a sample example of an e-commerce app which will notify both the customer and the related category managers when an order has been placed.

namespace App\Services;

use App\Entity\Employee;
use App\Entity\Order;
use App\Events\OrderEvent;
use App\Events\OrderEvents;
use BenTools\WebPushBundle\Model\Message\PushNotification;
use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionManagerRegistry;
use BenTools\WebPushBundle\Sender\PushMessageSender;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class NotificationSenderListener implements EventSubscriberInterface
{
    /**
     * [@var](https://github.com/var) UserSubscriptionManagerRegistry
     */
    private $userSubscriptionManager;

    /**
     * [@var](https://github.com/var) PushMessageSender
     */
    private $sender;

    /**
     * NotificationSender constructor.
     * [@param](https://github.com/param) UserSubscriptionManagerRegistry $userSubscriptionManager
     * [@param](https://github.com/param) PushMessageSender               $sender
     */
    public function __construct(
        UserSubscriptionManagerRegistry $userSubscriptionManager,
        PushMessageSender $sender
    ) {
        $this->userSubscriptionManager = $userSubscriptionManager;
        $this->sender = $sender;
    }

    public static function getSubscribedEvents()
    {
        return [
            OrderEvents::PLACED => 'onOrderPlaced',
        ];
    }

    /**
     * [@param](https://github.com/param) OrderEvent $event
     */
    public function onOrderPlaced(OrderEvent $event): void
    {
        $order = $event->getOrder();
        $this->notifyCustomer($order);
        $this->notifyCategoryManagers($order);
    }

    /**
     * [@param](https://github.com/param) Order $order
     */
    private function notifyCustomer(Order $order): void
    {
        $customer = $order->getCustomer();
        $subscriptions = $this->userSubscriptionManager->findByUser($customer);
        $notification = new PushNotification('Congratulations!', [
            PushNotification::BODY => 'Your order has been placed.',
            PushNotification::ICON => '/assets/icon_success.png',
        ]);
        $responses = $this->sender->push($notification->createMessage(), $subscriptions);

        foreach ($responses as $response) {
            if ($response->isExpired()) {
                $this->userSubscriptionManager->delete($response->getSubscription());
            }
        }
    }

    /**
     * [@param](https://github.com/param) Order $order
     */
    private function notifyCategoryManagers(Order $order): void
    {
        $products = $order->getProducts();
        $employees = [];
        foreach ($products as $product) {
            $employees[] = $product->getCategoryManager();
        }

        $employees = array_unique($employees);

        $subscriptions = [];
        foreach ($employees as $employee) {
            foreach ($this->userSubscriptionManager->findByUser($employee) as $subscription) {
                $subscriptions[] = $subscription;
            }
        }

        $notification = new PushNotification('A new order has been placed!', [
            PushNotification::BODY => 'A customer just bought some of your products.',
            PushNotification::ICON => '/assets/icon_success.png',
        ]);

        $responses = $this->sender->push($notification->createMessage(), $subscriptions);

        foreach ($responses as $response) {
            if ($response->isExpired()) {
                $this->userSubscriptionManager->delete($response->getSubscription());
            }
        }
    }
}

Previous: Configuration

Next: F.A.Q.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky