Using user notifications [Beta]
[New in Episerver CMS Core 9.4]
Note: This is a pre-release API that is UNSTABLE and might not satisfy the compatibility requirements as denoted by its associated normal version.
The User Notifications framework is intended for sending user-to-user notification messages.
A message is posted to the message queue through the Notifier. The message queue is handled by a scheduled job which periodically calls the NotificationDispatcher. The NotificationDispatcher formats the message using a NotificationFormatter and then sends it using a NotificationProvider. You can create your own formatters and providers.
Every message is sent on a channel (identified by a channel name), which is a namespace that groups messages of a certain kind together. Messages in different channels are not mixed. You can create your own channel by coming up with a name to use when creating messages and providing a formatter that supports it.
The sender has no control of how the recipient receives the message. The recipient’s provider preferences for that channel determine how the message is actually sent (at the moment a default preference resolver should be used).
Note: Do not use generic names for channels, providers and formatters because they risk colliding with other names. Prefix the names with something unique.
Message types
- NotificationMessage. The default message type; handles the message at the next scheduled job.
- ScheduledNotificationMessage. Handles the message at the next scheduled job that occurs after the provided time.
- DelayedNotificationMessage. Handles the message at the next scheduled job that occurs after the provided time but provides a delay time instead of an actual time.
Users
A user is an INotificationUser instance with a name and a display name.
To find users, get an instance of QueryableNotificationUserService and use the Find method to find users by part of a name or the Get method to get a specific user by name.
User preferences
Use an instance of INotificationPreferenceRegister to register a default preference resolver for a channel and a provider. The resolver takes a user's name and returns a provider-specific address (for example, email addresses for the email provider).
Notification posting
Get an instance of the INotifier interface and use the PostNotification method to post a message (for one or more recipients) to the message queue. If a message has more than one recipient, it is stored internally as one message for each recipient. Hook into the NotificationPosted, NotificationSaved and NotificationFiltered events to get feedback on the result of the posting (per message).
Notification dispatching
The NotificationDispatcher is configured in and triggered by the scheduler. It retrieves the messages that are waiting to be sent and groups them by recipient and channel. For each group, a formatter and a provider is located (if available) to format and send the messages. If there is a problem sending the messages, they are put back in the waiting queue.
Formatters
When one (or more) messages are sent, they first are routed through a formatter, which can transform the messages with custom formatting and/or combining several messages into one. A formatter has a name to identify it and a list of names that specifies which channels the formatter supports. If the message has a channel name that matches one of the supported names, that formatter is selected to format the message.
Note: If more than one formatter is found that matches the channel name, no message is sent and an error message is logged.
Formatting messages
To create your own formatter, implement the INotificationFormatter interface, give it a name and a list of supported channels. The FormatMessages method formats, takes, and returns a list of messages. In the simplest case of formatting, the method should just return the incoming messages. It also can format each message by changing Content (and Subject) or combine several messages into one. Make sure to set the ContainedIDs to match the IDs of the combined messages. Message IDs that exist in an in-message but not in an out-message are considered completed and removed from the queue. If there is a problem formatting the messages, the formatter throws an exception and those messages are put back into the queue and tried again at the next scheduled job.
Providers
A provider is a component for sending notifications using a specific method of communication. The built-in EmailNotificationProvider, for example, handles the sending of emails. A provider has a name to identify it and a format-property that specifies the format the provider supports (max length and if it supports HTML).
You can write your own provider by implementing the INotificationProvider interface and optionally the INotificationProviderStatus interface, which lets you enable and disable a provider. The Send method handles the sending of messages, and you should call succeededAction or failedAction for each message.
Handling notifications
Listing Notifications
Use the IUserNotificationRepository interface to list notifications with the GetUserNotification, GetUserNotifications and GetUserNotificationsCount methods. These methods are used in the Episerver user interface to show notifications.
To get formatted messages, extend the INotificationFormatter implementation with IUserNotificationFormatter and implement the FormatUserMessage method. When calling GetUserNotification(s), formatting is done depending on the formattingMode parameter.
It is possible to hook into the UserNotificationMissing event to get a callback when a specified notification cannot be found.
Marking
You can also use the IUserNotificationRepository interface to mark one or more notifications as read with one of the MarkUserNotification(s)AsRead methods. To listen to read notifications, hook into the UserNotificationRead and UserNotificationReads events.
Truncate message queue
Notifications are stored in the database and old notifications are deleted by the Notification Message Truncate scheduled job, which is set to run every night by default and removes all notifications older than 3 months.
Last updated: Dec 04, 2015