irishdan/notification-bundle

Symfony框架的通知包

v1.0.0 2018-08-14 17:02 UTC

This package is auto-updated.

Last update: 2024-09-16 20:08:49 UTC


README

Build Status Scrutinizer Code Quality

概述

此NotificationBundle允许通过多个渠道轻松创建和发送消息给用户或其他来源,同时也能轻松广播数据或消息。

无需配置即可发送通知

与FoundationInk Bundle结合使用,可以发送美观的HTML电子邮件。与taostr结合使用推送频道,以实现即时且引人注目的通知。结合推送频道和数据库频道,实现简单的直接消息。

基本设置

无需配置即可使用NotificationImage bundle。

步骤1:下载并启用包

使用composer下载

composer require irishdan/notification-bundle

在kernel中启用包

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new IrishDan\NotificationBundle\NotificationBundle(),
    );
}

步骤2:配置一些通知频道

默认情况下,该包支持多个频道,包括

要启用频道,只需将其配置添加到您的config.yml中。

# app/config/config.yml
notification:
    mail:
        default_sender: 'hi@jim.bob'
    database:
        entity: 'AppBundle:Notification'
    pusher:
        app_id: "12"
        auth_key: "1111SECURE222KEY"
        secret: "SeCrEt"
        cluster: "eu"
        encrypted: true
        event: 'notification'
        channel_name: 'private-direct_' # This is a private channel
    slack:
    nexmo:
        api_key: 7654321
        api_secret: oiCHOIoi
        from: "YourApp"
    logger:
        severity: 'info'

还可以创建自定义频道修改现有频道的行为

步骤4:数据库、Pusher、Nexmo和Slack频道有额外的步骤。

某些频道需要额外步骤

数据库频道

数据库频道实际上是将Doctrine实体持久化到数据库中。提供了一个生成器来创建实体。

php bin/console notification:create-database-notification 

Pusher频道

Pusher是一个提供合理免费套餐的第三方服务。您需要有效的pusher凭证才能使用该频道。

还需要pusher PHP库。使用composer安装

composer require pusher/pusher-php-server

如果您使用私有频道(强烈推荐),则需要pusher身份验证路由。将路由导入到您的项目中

# app/config/routing.yml
notification_pusher_auth:
    resource: "@NotificationBundle/Resources/config/routing.yml"

Pusher需要一个javascript库,并且为了与您定义的pusher频道交互,您还需要额外的步骤。提供了生成所需javascript的Twig函数

{% block javascripts %}
    <script src="https://js.pusher.com/4.0/pusher.min.js"></script>
    <script>
      
        // Enable pusher logging - don't include this in production
        Pusher.logToConsole = true;

        {{ notification_new_pusher_js() }}

        {{ notification_new_pusher_channel_js(app.user) }}

        channel.bind('{{ notification_pusher_event }}', function (data) {
            // The data object contains your notification data
            console.log(data);
            // Add custom js to react to the the notifcation.
            // A good solution is to use toastr to display the notification.
        });  
    </script>
{% endblock %}

Nexmo频道

Nexmo是一个第三方短信服务。您需要有效的凭证才能使用此频道。

Slack频道

Slack

步骤5:将用户订阅到一个或多个频道

为了使用您配置的频道发送通知,用户必须订阅每个频道。

假设您的用户类是AppBundle\Entity\User,实现所需的接口:@TODO:改进此

<?php

namespace AppBundle\Entity;

use IrishDan\NotificationBundle\Notification\NotifiableInterface;
use IrishDan\NotificationBundle\PusherableInterface;
use IrishDan\NotificationBundle\SlackableInterface;
use IrishDan\NotificationBundle\TextableInterface;
use IrishDan\NotificationBundle\EmailableInterface;

class User implements UserInterface, NotifiableInterface, EmailableInterface, TextableInterface, PusherableInterface, SlackableInterface, DatabaseNotifiableInterface

    // For convenience use the
    use FullyNofifiableTrait();

步骤6:生成通知对象

每个通知都是一个单独的对象。例如,您可能有一个NewMemberNotification()对象和一个NewPaymentReceivedNotification()对象。

要创建新的通知对象,请使用提供的生成器。

php bin/console notification:create

步骤7:编辑通知内容

使用twig...

步骤8:发送通知

要发送通知,您需要一个收件人。

接收者是对实现了NotifiableInterface的对象,在第5步中创建。通知是对实现了Notification接口的对象,在第6步中生成。

可以使用 'notification.manager' 服务发送通知,如下所示

<?php

/** $user NotifiableInterface */
$user = $this->getUser();

/** $notification NotificationInterface */
$notification = new NewMemberNotification();

// The notification.manager service is used to send notifications
$this->get('notification.manager')->send($notification, $user);

// You can send to multiple users also.
$this->get('notification.manager')->send($notification, [$user1, $user2]);

// You can pass extra data into the notification also
// This will be available in the data array
// and also in twig templates as {{ data.date }}
$data = [
    'date' => new \DateTime(),
];
$this->get('notification.manager')->send($notification, $user, $data);

文档

要了解更高级的设置,请参阅文档

归属

  • 此组件受到了Laravel通知系统的启发