bentools / pusher-bundle
bentools/pusher 的 Symfony 桥接器
v0.4
2017-02-08 15:56 UTC
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-09-15 01:20:13 UTC
README
⚠️ 此包已被弃用,推荐使用 bentools/webpush-bundle
Pusher Bundle
此包封装了 bentools/pusher 库,以便通过您的 Symfony 应用程序发送推送通知。
安装
安装库
composer require bentools/pusher-bundle
将 Bundle 添加到您的 AppKernel
# app/AppKernel.php $bundles = [ // ... new BenTools\PusherBundle\BenToolsPusherBundle(), // ... ];
在您的 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%']
更新您的数据库以存储推送订阅
php bin/console doctrine:schema:update --dump-sql --force
# or use Doctrine Migrations
挂载 /webpush/registration
路由
# app/config/routing.yml webpush_bundle: resource: "@BenToolsPusherBundle/Resources/config/routing.yml" prefix: /webpush
安装 JS 资产
php bin/console assets:install --symlink
在 twig 中嵌入(HTTPS 或 localhost,且需要认证用户)
<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>
用法
您的 BenTools\PusherBundle\Entity\Recipient
实体实现了 BenTools\Pusher\Model\Recipient\RecipientInterface
,可用于 Pusher。
PHP 示例代码
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);
许可证
MIT