bentools / pusher-bundle
Symfony bridge for bentools/pusher
Installs: 50
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 1
Type:symfony-bundle
Requires
- bentools/pusher: 0.4.*
- piwik/device-detector: ^3.7
Requires (Dev)
- symfony/symfony: ^2.8|^3.0
This package is auto-updated.
Last update: 2024-12-15 01:54:22 UTC
README
⚠️ 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