Using user notifications [Beta]
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 INotifier (Notifier in versions prior to 10.3). From versions 10.3, a message can either be configured to be sent immediately or be placed in a message queue that is periodically handled by a scheduled job. (In versions prior to 10.3, the message queue was handled by the scheduled job only.) Messages to be sent are passed to 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. From version 10.3, it is possible to configure if messages for a specific channel should be sent immediately by adding an instance of NotificationChannelOptions to INotificationChannelOptionsRegistry specifing that messages within this channel should be processed immediately. By default are messages processed periodically by the scheduler.
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.
- NotificationMessage. The default message type; handles the message at the next scheduled job or (from version 10.3) immediately depending on registered options for the channel.
- 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.
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.
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).
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).
The NotificationDispatcher is configured in and triggered by the scheduler or (from version 10.3) called directly for immediate messages. 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.
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.
To create your own formatter, implement the INotificationFormatter interface, give it a name and a list of supported channels. The FormatMessagesAsync method (FormatMessages in versions prior to 10.3) 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.
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 SendAsync method (Send in versions prior to 10.3) handles the sending of messages, and you should call succeededAction or failedAction for each message.
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 FormatUserMessageAsync method (FormatUserMessage in versions prior to 10.3). 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.
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.