Registering New Notification Type¶
This guide describes the basic steps needed to define a new email notification type, which includes logging it in the notification registry.
For your convenience, you can find the source code of the sample project here.
Prerequisites¶
Prior to registering your notifications, you need to configure the notification gateway, which we explain here.
Defining Custom Email Notification Type¶
First, you need to create a notification and give it a name, e.g., SampleEmailNotification
, basing it on the EmailNotification
class (as opposed to another standard class, SmsNotification
):
public class SampleEmailNotification : EmailNotification
{
public SampleEmailNotification() : base(nameof(SampleEmailNotification)) {}
public string Greeting { get; set; }
}
Registration in Notification Registry¶
Your next step is registering your SampleEmailNotification
in the notification registry:
Notes to the code:
Line 4: Getting the instance of INotificationRegistrar
type that stores all known notification types used in the system.
Line 5: Registering SampleEmailNotification
and setting a default template by running the WithTemplates
extension method. In Template.Subject
, we use the {greeting}
liquid expression that will be replaced with the Greeting
property value of the SampleEmailNotification
class instance after rendering.
Notification Localization¶
Virto Platform Manager supports localization resources for notification types. This is achieved by adding a JSON object to the notificationTypes
section, the key having the same name as the notification type:
"notificationTypes": {
...
"SampleEmailNotification": {
"displayName": "Sample email notification",
"description": "Some sample email description"
},
...
}
Sending Notifications¶
In order to send a notification from your code, you need to use two interfaces: INotificationSearchService
and INotificationSender
.
Let's show how you can send a notification basing on this code sample:
|
Notes to the code:
Line 15: Constructing a new instance of the SampleEmailNotification
type by calling the INotificationSearchService.GetNotificationAsync<>
extension method.
Lines 16 to 19: Populating the required email notification properties, such as From
and To
, and setting a value for our custom Greeting
property; this value will be eventually interpolated in the email subject, e.g. Hi { greeting } → Hi John.