Notifications
The platform provides interaction mechanisms, one of which is the notification system.
How to create a new notification
First, we need to create an event extended from NotificationEvent
.
See the example.
public class BirthdayNotificationEvent extends NotificationEvent {
// ------------------------------ FIELDS ------------------------------
private String name;
// --------------------- GETTER / SETTER METHODS ---------------------
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
After that, we need to create an implementation for each type of notification that we want, like email, message or SMS.
Each implementation will be visible in the configuration dialog. This allows the user to choose whether to receive this notification.
For our example, we will send a message wishing a happy birthday.
So we need to implement the interface named NotificationListener
.
The annotation Notification
is responsible to draw the configuration grid.
For this reason, you should choose the appropriated category.
See the example.
@Named
@Notification(category = "User", event = "Happy Birthday", type = "notification.type.message")
public class BirthdayMessageNotificationListener implements NotificationListener<BirthdayNotificationEvent> {
// ------------------------------ FIELDS ------------------------------
@Inject
private NotificationService notificationService;
// ------------------------ INTERFACE METHODS ------------------------
// --------------------- Interface NotificationListener ---------------------
@Override
public void execute(BirthdayNotificationEvent event, List<UserHumanBeing> recipients) {
NotificationOptions options = new NotificationOptions();
Map<String, Object> data = new HashMap<>();
data.put("name", event.getName());
options.setRenderer("BirthdayNotificationRenderer");
options.setData(data);
options.setRecipients(recipients);
notificationService.addNotification(options);
}
}
The messages needs an client-side implementation.
You may notice a class called BirthdayNotificationRenderer
was defined in the renderer parameter.
So, you should create a implementation extended of BaseNotificationRenderer
.
Following our example, we need to create the message that will show the warning about the birthday.
class @BirthdayNotificationRenderer extends BaseNotificationRenderer
render: ($container, data) ->
$container.append "Happy Birthday #{data.data.name}!"
Finally, we can send the notification.
For that, we need to use the NotificationService
.
@Service
public class BirthdayService {
// ------------------------------ FIELDS ------------------------------
@Inject
private NotificationService notificationService;
// -------------------------- OTHER METHODS --------------------------
public void notify(UserHumanBeing humanBeing) {
BirthdayNotificationEvent event = new BirthdayNotificationEvent();
event.setName(humanBeing.getName());
event.getRecipients().add(humanBeing);
notificationService.notify(event);
}
}
After that, the user will receive the following message.
Enable / Disable Notification through app.xml
It's possible to enable/disable Notification through app.xml
. To do this, you need two informations: event and type.
Event: event
property from @Notification
annotation, using the example above: Happy Birthday
. This information is also displayed as tooltip at event list from notification configuration page (Custom Root Configuration - Administrative - Notifications), this is useful for CEAP events.
Type: notification.type.message
or notification.type.mail
.
app.xml
configuration example:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:configuration xmlns:ns2="http://meceap.me.com.br/schema/config-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://meceap.me.com.br/schema/do-mapping http://docs.miisy.me/xsd/do-mapping.xsd">
<notifications>
<notification event="notification.event.task.assignment" type="notification.type.message" enabled="false"/>
<notification event="notification.event.task.assignment" type="notification.type.mail" enabled="false"/>
</notifications>
</ns2:configuration>
This example disable Task Assignment notifications for message and mail.