⚠️ This bundle is now deprecated in favor of bentools/webpush-bundle, (*1)
Pusher Bundle
This bundle wraps the bentools/pusher library to send Push Notifications through your Symfony application., (*2)
Installation
Install the library:, (*3)
composer require bentools/pusher-bundle
Add the Bundle into your AppKernel
:, (*4)
# app/AppKernel.php
$bundles = [
// ...
new BenTools\PusherBundle\BenToolsPusherBundle(),
// ...
];
Define your handlers in your services.yml
:, (*5)
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:, (*6)
php bin/console doctrine:schema:update --dump-sql --force
# or use Doctrine Migrations
Mount the /webpush/registration
route:, (*7)
# app/config/routing.yml
webpush_bundle:
resource: "@BenToolsPusherBundle/Resources/config/routing.yml"
prefix: /webpush
Install the JS assets:, (*8)
php bin/console assets:install --symlink
Embed in twig (HTTPS or localhost, and authenticated user required), (*9)
Usage
Your BenTools\PusherBundle\Entity\Recipient
entities implement BenTools\Pusher\Model\Recipient\RecipientInterface
and can be used in Pusher., (*10)
PHP snippet to help:, (*11)
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, (*12)