创建通知

可以通过管理界面或者编程式创建通知。

管理界面

在应用程序安装此扩展组件之后,主菜单会包含 Notifications(通知消息) 项。Notifications browser(通知消息) 浏览界面展示创建的通知列表以及管理通知的按钮。

ntf list

点击 Create new notification(新建通知) 按钮后会展示通知编辑界面。

ntf editor

有下列字段:

  • Subject(主题) - 通知的主题。

  • Type(类型) - 通知类型。组件默认不带任何通知类型,参阅 配置 章节创建通知类型。

  • Recipients(收件人) - 通知的收件人列表。在该字段内输入 username,然后可以在列表中选择合适的用户。

  • Channels(通道) - 发送通知的通道。

    • In-app channel(站内通道) - 用户在应用程序界面收到通知。

    • Email channel(邮件通道) - 用户收到邮件通知。需要安装 电子邮件 扩展组件。

  • Body(正文) - 通知正文。

编程式创建

NotificationManager 服务提供给用户发送通知的方法。

下面示例中,每当新订单(Order)创建时,会给用户发送通知。

ntf programmatic
private boolean justCreated;

@ViewComponent
private TypedTextField<Integer> numberField;

@Autowired
protected NotificationManager notificationManager;

@Subscribe
public void onInitEntity(InitEntityEvent<Order> event) {
    justCreated = true;
}

@Subscribe(target = Target.DATA_CONTEXT)
public void onPostSave(final DataContext.PostSaveEvent event) { (1)
    if (justCreated) { (2)
        notificationManager.createNotification() (3)
                .withSubject("New order")(4)
                .withRecipientUsernames("admin") (5)
                .toChannelsByNames("in-app") (6)
                .withContentType(ContentType.PLAIN) (7)
                .withBody("A new order with number " + numberField.getValue()+ " is created.") (8)
                .send(); (9)
    }
}
1 保存更改后调用该方法。
2 检查该实体是否新建。
3 初始化 NotificationRequestBuilder,创建一个 NotificationRequest 对象。
4 定义通知的主题。
5 用提供的 username 设置收件人。
6 用名称确定通知的发送通道,可以使用 in-appemail
7 设置通知正文内容类型。
8 设置通知正文。
9 创建并发送通知请求。