jeffersonsimaogoncalves/cakephp-notifier

CakePHP 的通知插件

安装: 280

依赖项: 0

建议者: 0

安全: 0

星级: 1

关注者: 2

分支: 38

类型:cakephp-plugin

2.2.0 2020-08-18 03:06 UTC

This package is auto-updated.

Last update: 2024-09-18 11:49:55 UTC


README

此插件允许您将简单的通知系统集成到您的应用程序中。

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

将此插件作为 composer 包安装的推荐方式是

    composer require jeffersonsimaogoncalves/cakephp-notifier

现在,使用以下命令加载插件

    bin/cake plugin load -b JeffersonSimaoGoncalves/Notifier

加载插件后,您需要使用以下命令迁移插件的表

    bin/cake migrations migrate -p JeffersonSimaoGoncalves/Notifier

发送通知

模板

在发送任何通知之前,我们需要注册一个模板。以下是如何添加模板的示例

    $notificationManager->addTemplate('newBlog', [
        'title' => 'New blog by :username',
        'body' => ':username has posted a new blog named :name'
    ]);

添加新模板时,您必须添加一个 title 和一个 body。两者都可以包含变量,如 :username:name。稍后我们将详细介绍这些变量。

通知

现在我们可以使用我们的 newBlog 模板发送新的通知。

    $notificationManager->notify([
        'users' => ['7be18ce3-94e6-476a-870d-a0108d1697b7', 'c72da51b-16c8-4741-8646-2e45721b3277'],
        'recipientLists' => ['administrators'],
        'template' => 'newBlog',
        'vars' => [
            'username' => 'Bob Mulder',
            'name' => 'My great new blogpost'
        ]
    ]);

注意:您还可以通过组件发送通知: $this->Notifier->notify()

使用 notify 方法我们发送了新的通知。以下是一些所有属性的列表

  • users - 这是一个整数或包含用户 ID 的数组,用于通知。因此,当您想通知用户 c72da51b-16c8-4741-8646-2e45721b3277 和 7be18ce3-94e6-476a-870d-a0108d1697b7 时,请添加 [c72da51b-16c8-4741-8646-2e45721b3277, 7be18ce3-94e6-476a-870d-a0108d1697b7]
  • recipientLists - 这是一个字符串或包含收件人列表的数组。稍后您将了解更多关于 RecipientLists 的信息。
  • template - 您添加的模板,例如 newBlog
  • vars - 要使用的变量。在模板 newBlog 中,我们使用了变量 usernamename。这些变量可以在此处定义。

收件人列表

要向大型组发送通知,您可以使用 RecipientLists。您可以使用以下方式注册它们

    $notificationManager->addRecipientList('administrators', ['7be18ce3-94e6-476a-870d-a0108d1697b7', 'c72da51b-16c8-4741-8646-2e45721b3277','7be18ce3-94e6-476a-870d-a0108d1697b4', 'c72da51b-16c8-4741-8646-2e45721b3272']);

现在我们已经创建了一个名为 administrators 的收件人列表。

这可以在我们发送新通知时使用。

    $notificationManager->notify([
        'recipientLists' => ['administrators'],
    ]);

现在,用户 7be18ce3-94e6-476a-870d-a0108d1697b7、c72da51b-16c8-4741-8646-2e45721b3277、7be18ce3-94e6-476a-870d-a0108d1697b4 和 c72da51b-16c8-4741-8646-2e45721b3272 将收到通知。

检索通知

列表

您可以通过 getNotifications 方法轻松检索通知。以下是一些示例

    // getting a list of all notifications of the current logged in user
    $this->Notifier->getNotifications();

    // getting a list of all notifications of the user with id c72da51b-16c8-4741-8646-2e45721b3272
    $this->Notifier->getNotifications('c72da51b-16c8-4741-8646-2e45721b3272');
    
    // getting a list of all unread notifications
    $this->Notifier->allNotificationList('c72da51b-16c8-4741-8646-2e45721b3272', true);

    // getting a list of all read notifications
    $this->Notifier->allNotificationList('c72da51b-16c8-4741-8646-2e45721b3272', false);

计数

可以通过 countNotifications 方法获取已读/未读通知的计数。以下是一些示例

    // getting a number of all notifications of the current logged in user
    $this->Notifier->countNotifications();

    // getting a number of all notifications of the user with id c72da51b-16c8-4741-8646-2e45721b3272
    $this->Notifier->countNotifications('c72da51b-16c8-4741-8646-2e45721b3272');
    
    // getting a number of all unread notifications
    $this->Notifier->countNotificationList('c72da51b-16c8-4741-8646-2e45721b3272', true);

    // getting a number of all read notifications
    $this->Notifier->countNotificationList('c72da51b-16c8-4741-8646-2e45721b3272', false);

标记为已读

要将通知标记为已读,您可以使用 markAsRead 方法。以下是一些示例

    // mark a single notification as read
    $this->Notifier->markAsRead(500);

    // mark all notifications of the given user as read
    $this->Notifier->markAsRead(null, 'c72da51b-16c8-4741-8646-2e45721b3272');

通知实体

以下获取器可以在您的通知实体中使用

  • title - 包含变量的生成标题。
  • body - 包含变量的生成正文。
  • unread - 如果通知尚未阅读,则为布尔值。
  • read - 如果通知已阅读,则为布尔值。

示例

    // returns true or false
    $entity->get('unread');
    
    // returns the full output like 'Bob Mulder has posted a new blog named My Great New Post'
    $entity->get('body');

传递到视图

您可以使用以下方式在视图中使用通知列表

    $this->set('notifications', $this->Notifier->getNotifications());

通知管理器

NotificationManager 是插件的 Manager。您可以使用以下方式获取其实例

    NotificationManager::instance();

NotificationManager 的命名空间为 JeffersonSimaoGoncalves\Notifier\Utility\NotificationManager

Manager 有以下方法可用

  • notify
  • addRecipientList
  • getRecipientList
  • addTemplate
  • getTemplate

通知组件

JeffersonSimaoGoncalves/Notifier.Notifier 组件可以在控制器中使用

    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('JeffersonSimaoGoncalves/Notifier.Notifier');
    }

组件有以下方法可用

  • getNotifications
  • countNotifications
  • markAsRead
  • notify

保持联系

我们非常欢迎拉取请求!

致谢

这项工作基于 Bakkerij 的代码,请参阅相关代码