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.
1public abstract class MessageFormatter<2S extends Subscription<C>,3C extends Recordable,4D extends DeliveryOption<M>,5M extends Message6> extends Record {78public abstract M format(MessageContext<S, C> messageContext, D deliveryOption);910}
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.
1package com.psddev.cms.notification;23BrowserDeliveryOption<BrowserMessage>45EmailDeliveryOption<EmailMessage>67SmsDeliveryOption<SmsMessage>89SlackDeliveryOption<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.
1import com.psddev.cms.notification.MessageContext;2import com.psddev.cms.notification.MessageFormatter;3import org.slf4j.Logger;4import org.slf4j.LoggerFactory;56public class ToolUserAuthLogMessageFormatter7extends MessageFormatter<ToolUserAuthSubscription, ToolUserAuthEvent, LogDeliveryOption, LogMessage> {89private static final Logger LOGGER = LoggerFactory.getLogger(ToolUserAuthLogMessageFormatter.class);1011@Override12public LogMessage format(13MessageContext<ToolUserAuthSubscription, ToolUserAuthEvent> messageContext,14LogDeliveryOption deliveryOption) {1516ToolUserAuthEvent context = messageContext.getContext();17ToolUserAuthAction action = context.getAction();1819String receiverLabel = messageContext.getReceiver().getReceiverLabel();20String authUserLabel = context.getUserLabel();21String actionLabel;2223switch (action) {24case LOGIN:25actionLabel = "logged in to";26break;27case LOGOUT:28actionLabel = "logged out of";29break;30default:31return null;32}3334String message = String.format("%s was notified that %s %s the CMS", receiverLabel, authUserLabel, actionLabel);3536return new LogMessage(messageContext, LOGGER, LogLevel.WARN, message);37}38}
HTML formatting
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.