symiote/silverstripe-notifications

从代码中发送CMS可编辑的系统通知

安装次数: 5,671

依赖项: 3

建议者: 0

安全: 0

星标: 15

关注者: 9

分支: 13

开放问题: 0

类型:silverstripe-vendormodule

4.7.0 2022-11-11 04:47 UTC

README

从代码中发送CMS管理系统的电子邮件通知。

维护者联系方式

需求

  • SilverStripe 4.0+

安装说明

composer require symbiote/silverstripe-notifications

发送通知

该模块提供了一个默认的BroadcastNotification对象,可以用于一次向多人发送通知。首先,创建SystemNotification(这定义了通知将如何发送)

通知 > 添加系统通知

  • 标识符:BROADCAST
  • 标题:(你的自定义)
  • 相关于:BroadcastNotification
  • 通过以下渠道发送:内部
  • 文本:(你的自定义;使用$Context.Content输出广播内容)

通知 > 添加广播通知

  • 标题:(你的自定义)
  • 内容:(你的自定义)
  • 点击创建
  • 组:选择要接收通知的组
  • 现在发送:准备好发送通知时点击。

创建系统通知

创建自定义通知需要一些代码来组合。以下以BroadcastNotification为例,以下列出了关键点

1)

在你的_config.yml文件中,为每个需要的通知添加一个标识符。这允许你从代码中在数据库中查找Notification对象。

Symbiote\Notifications\Model\SystemNotification:
  identifiers:
    - 'NAME_OF_NOTIFICATION1'
    - 'NAME_OF_NOTIFICATION2'

2)

将NotifiedOn接口添加到任何与你要发送的通知相关的数据对象。这是必要的,以便Notifications模块可以在你的对象上查找以下方法以发送通知。

use Symbiote\Notifications\Model\NotifiedOn;

class MyDataObject extends DataObject implements NotifiedOn {
	...

在通知的对象上定义以下接口方法。

/**
 * Return a list of available keywords in the format 
 * array('keyword' => 'A description') to help users format notification fields
 * @return array
 */
public function getAvailableKeywords();
/**
 * Gets an associative array of data that can be accessed in
 * notification fields and templates 
 * @return array
 */
public function getNotificationTemplateData();

注意:以下模板数据将自动包含

  • $ThemeDirs(主题的ArrayList对象,如果你只有一个主题,使用$ThemeDirs.First应该与旧的$ThemeDir相同)
  • $SiteConfig
  • $MyDataObject(你的NotifiedOn数据对象的ClassName)
  • $Member(正在发送消息的Member对象)
/**
 * Gets the list of recipients for a given notification event, based on this object's 
 * state. 
 * $event The identifier of the event that triggered this notification
 * @return array An array of Member objects
 */
public function getRecipients($event);

注意:getRecipients()可以返回任何对象的数组,只要它们具有Email属性或方法

3)

在CMS的通知模型管理中创建通知。

4)

从你的代码中发送通知,其中$contextObject是正在通知的数据对象的实例

use Symbiote\Notifications\Service\NotificationService;

singleton(NotificationService::class)->notify('NOTIFICATION_IDENTIFIER', $contextObject);

模板

通知可以使用.ss模板进行渲染。如果你想在电子邮件通知中添加标题/页脚,这很有用。你可以在CMS中为每个通知指定一个模板,也可以为所有要渲染的通知设置一个默认模板

Symbiote\Notifications\Model\SystemNotification:
  default_template: EmailNotification

在你的模板中,使用$Body变量渲染通知文本。

配置

你可能需要配置一个发送_from的电子邮件地址-

Symbiote\Notifications\Service\EmailNotificationSender:
  send_notifications_from: 'notifications@example.com'

TODO

  • 使用QueuedJobs模块测试处理大量通知的可配置批次/队列