bentools/pusher-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bentools/pusher-bundle

Composer 安装命令:

composer require bentools/pusher-bundle

包简介

Symfony bridge for bentools/pusher

README 文档

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

统计信息

  • 总下载量: 50
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固