Class NotificationServices
To use the NotificationService, a message specific service should be defined for a particular Freemarker Template mapping the required fields of the template to the required attributes of the service.
This service definition should extend the sendNotificationInterface
or the prepareNotificationInterface
service interface
and simply invoke the associated method defined in this class.
<service name="sendPoPickupNotification" engine="java"
location="org.apache.ofbiz.content.email.NotificationServices"
invoke="sendNotification">
<description>Sends notification based on a message template</description>
<implements service="sendNotificationInterface"/>
<attribute name="orderId" type="String" mode="IN" optional="false"/>
</service>
An optional parameter available to all message templates is
baseUrl
which can either be specified when the service is
invoked or let the NotificationService
attempt to resolve it
as best it can, see setBaseUrl(Map)
for details on how this is achieved.
Please use the following link to schedule a delivery date: ${baseUrl}/ordermgr/control/schedulepo?orderId=${orderId}"
The template file must be found on the classpath at runtime and match the "templateName" field passed to the service when it is invoked.
For complex messages with a large number of dynamic fields, it may be wise
to implement a custom service that takes one or two parameters that can
be used to resolve the rest of the required fields and then pass them to
the prepareNotification(DispatchContext, Map)
or sendNotification(DispatchContext, Map)
methods directly to generate or generate and send the notification respectively.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprepareNotification
(DispatchContext ctx, Map<String, ? extends Object> context) This will process the associated notification template definition with all the fields contained in the given context and generate the message body of the notification.sendNotification
(DispatchContext ctx, Map<String, ? extends Object> context) This will use theprepareNotification(DispatchContext, Map)
method to generate the body of the notification message to send and then deliver it via the "sendMail" service.static void
The expectation is that a lot of notification messages will include a link back to one or more pages in the system, which will require knowledge of the base URL to extrapolate.
-
Constructor Details
-
NotificationServices
public NotificationServices()
-
-
Method Details
-
sendNotification
public static Map<String,Object> sendNotification(DispatchContext ctx, Map<String, ? extends Object> context) This will use theprepareNotification(DispatchContext, Map)
method to generate the body of the notification message to send and then deliver it via the "sendMail" service.If the "body" parameter is already specified, the message body generation phase will be skipped and the notification will be sent with the specified body instead. This can be used to combine both service calls in a decoupled manner if other steps are required between generating the message body and sending the notification.
- Parameters:
ctx
- The dispatching context of the servicecontext
- The map containing all the fields associated with the sevice- Returns:
- A Map with the service response messages in it
-
prepareNotification
public static Map<String,Object> prepareNotification(DispatchContext ctx, Map<String, ? extends Object> context) This will process the associated notification template definition with all the fields contained in the given context and generate the message body of the notification.The result returned will contain the appropriate response messages indicating success or failure and the OUT parameter, "body" containing the generated message.
- Parameters:
ctx
- The dispatching context of the servicecontext
- The map containing all the fields associated with the sevice- Returns:
- A new Map indicating success or error containing the body generated from the template and the input parameters.
-
setBaseUrl
The expectation is that a lot of notification messages will include a link back to one or more pages in the system, which will require knowledge of the base URL to extrapolate. This method will ensure that thebaseUrl
field is set in the given context.If it has been specified a default
baseUrl
will be set using a best effort approach. If it is specified in the url.properties configuration files of the system, that will be used, otherwise it will attempt resolve the fully qualified local host name.Note: I thought it might be useful to have some dynamic way of extending the default properties provided by the NotificationService, such as the
baseUrl
, perhaps using the standardResourceBundle
java approach so that both classes and static files may be invoked.- Parameters:
context
- The context to check and, if necessary, set thebaseUrl
.
-