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

Pusher Bundle Laravel Package

bentools/pusher-bundle

View on GitHub
Deep Wiki
Context7

⚠️ This bundle is now deprecated in favor of bentools/webpush-bundle

Pusher Bundle

This bundle wraps the bentools/pusher library to send Push Notifications through your Symfony application.

Installation

Install the library:

composer require bentools/pusher-bundle

Add the Bundle into your AppKernel:

# app/AppKernel.php
$bundles = [
    // ...
    new BenTools\PusherBundle\BenToolsPusherBundle(),
    // ...
];

Define your handlers in your services.yml:

services:
    bentools.pusher.handler.mozilla:
        class: BenTools\Pusher\Model\Handler\MozillaHandler
        arguments: ['@guzzle.client.default']

    bentools.pusher.handler.gcm:
        class: BenTools\Pusher\Model\Handler\GoogleCloudMessagingHandler
        arguments: ['@guzzle.client.default', '%gcm.api_key%']

Update your database to store push subscriptions:

php bin/console doctrine:schema:update --dump-sql --force
# or use Doctrine Migrations

Mount the /webpush/registration route:

# app/config/routing.yml
webpush_bundle:
    resource: "@BenToolsPusherBundle/Resources/config/routing.yml"
    prefix:   /webpush

Install the JS assets:

php bin/console assets:install --symlink

Embed in twig (HTTPS or localhost, and authenticated user required)

<script src="{{ asset('bundles/bentoolspusher/js/pushClient.js') }}"></script>
<script>

    var registrationUrl = '{{ path('webpushbundle_register') }}';

    new subscriber({

        'onServerMessage': function(event, serverMessage) {
            console.log(event);
        },

        'onExistingSubscription' : function(subscription) {
            console.log('Existing subscription', subscription);
        },

        'onNewSubscription' : function(subscription) {
            console.log('New subscription', subscription);
            return fetch(registrationUrl, {
                method     : 'POST',
                mode       : 'cors',
                credentials: 'include',
                cache      : 'default',
                headers    : new Headers({
                    'Accept'      : 'application/json',
                    'Content-Type': 'application/json'
                }),
                body       : JSON.stringify({subscription: subscription})
            });
        }
    });

</script>

Usage

Your BenTools\PusherBundle\Entity\Recipient entities implement BenTools\Pusher\Model\Recipient\RecipientInterface and can be used in Pusher.

PHP snippet to help:

use AppBundle\Entity\User;
use BenTools\Pusher\Model\Message\Notification;
use BenTools\Pusher\Model\Push\Push;
use BenTools\PusherBundle\Entity\Recipient;

$pusher     = $this->getContainer()->get('bentools.pusher');
$user       = $this->getRepositoryOf(User::class)->findOneBy([
    'username' => 'johndoe',
]);
$recipients = $this->getContainer()->get('doctrine')->getManager()->getRepository(Recipient::class)->findRecipientsForUser($user);
$message    = new Notification('Ho hi');
$push       = new Push();

foreach ($recipients AS $recipient) {
    switch ($recipient->getClient()) {
        case Recipient::CHROME:
        case Recipient::CHROME_MOBILE:
            $push->addRecipient($recipient, $this->getContainer()->get('bentools.pusher.handler.gcm'));
            break;
        case Recipient::FIREFOX:
            $push->addRecipient($recipient, $this->getContainer()->get('bentools.pusher.handler.mozilla'));
            break;
    }

    $push->setMessage($message);

}

$pusher->push($push);

License

MIT

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.
directorytree/privacy-filter-classifier
directorytree/privacy-filter
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit