Formatting notification messages
The Notification system requires that all subscriptions come with default message formats for both plain text and HTML, and they should contain all of the relevant information a subscriber of some event might care about. Furthermore, it is common practice to specify a custom presentation for the various notifications. As such, Brightspot provides the MessageFormatter
class to customize a notification’s presentation over a given delivery option.
public abstract class MessageFormatter<
S extends Subscription<C>,
C extends Recordable,
D extends DeliveryOption<M>,
M extends Message
> extends Record {
public abstract M format(MessageContext<S, C> messageContext, D deliveryOption);
}
Each message formatter implementation is specific to a type of subscription and a type of delivery option. This means that if you want a custom message format for two different subscription types across three different delivery options, you could have a total of six message formatter implementations. All you need to do is create the classes that you need with the correct Java generics typing, and Brightspot will take care of the rest. Which message formatters get applied is controlled editorially in Brightspot through the global settings.
Each delivery option delivers a specific message type. See the mapping below as a reference.
package com.psddev.cms.notification;
BrowserDeliveryOption<BrowserMessage>
EmailDeliveryOption<EmailMessage>
SmsDeliveryOption<SmsMessage>
SlackDeliveryOption<com.psddev.slack.SlackMessage>
The following snippet is a custom formatter that uses a custom logger, forces the log level to always be a WARNING, and changes the log message.
import com.psddev.cms.notification.MessageContext;
import com.psddev.cms.notification.MessageFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ToolUserAuthLogMessageFormatter
extends MessageFormatter<ToolUserAuthSubscription, ToolUserAuthEvent, LogDeliveryOption, LogMessage> {
private static final Logger LOGGER = LoggerFactory.getLogger(ToolUserAuthLogMessageFormatter.class);
@Override
public LogMessage format(
MessageContext<ToolUserAuthSubscription, ToolUserAuthEvent> messageContext,
LogDeliveryOption deliveryOption) {
ToolUserAuthEvent context = messageContext.getContext();
ToolUserAuthAction action = context.getAction();
String receiverLabel = messageContext.getReceiver().getReceiverLabel();
String authUserLabel = context.getUserLabel();
String actionLabel;
switch (action) {
case LOGIN:
actionLabel = "logged in to";
break;
case LOGOUT:
actionLabel = "logged out of";
break;
default:
return null;
}
String message = String.format("%s was notified that %s %s the CMS", receiverLabel, authUserLabel, actionLabel);
return new LogMessage(messageContext, LOGGER, LogLevel.WARN, message);
}
}
The only special element honored by the built-in delivery options is the <content>
element, which contains a data-id attribute corresponding to the UUID of an item in the database. Delivery options will search for these elements and convert them into a delivery-option-specific link that opens the item in the content edit page.