Overview
The documentation describes how to get notifications, templates and how to send.
How to register own notification
If you would like to register a notification e.g. 'SampleEmailNotification': 1. Create the notification with name 'SampleEmailNotification' and based on 'EmailNotification' (also there is a standard based class 'SmsNotification')
public class SampleEmailNotification : EmailNotification
{
public SampleEmailNotification() : base(nameof(SampleEmailNotification)) {}
}
INotificationRegistrar
in PostInitialize
method of Module.cs
.
var registrar = appBuilder.ApplicationServices.GetService<INotificationRegistrar>();
RegisterNotification
and set generic type as SampleEmailNotification
registrar.RegisterNotification<SampleEmailNotification>();
How to define a template for the notification
-
If need to use predefined templates then
- use construction:
registrar.RegisterNotification<SampleEmailNotification>().WithTemplates(new EmailNotificationTemplate() { Subject = "Sample subject", Body = "<p>Sample text</p>", });
look at the code
- or use resources like this
var assembly = Assembly.GetExecutingAssembly(); registrar.RegisterNotification<SampleEmailNotification>().WithTemplates(new EmailNotificationTemplate() { Subject = assembly.GetManifestResourceStream("VirtoCommerce.NotificationsSampleModule.Web.Templates.SampleEmailNotification_subject.txt").ReadToString(), Body = assembly.GetManifestResourceStream("VirtoCommerce.NotificationsSampleModule.Web.Templates.SampleEmailNotification_body.html").ReadToString() });
and add the resources to 'Templates' folder (look at samples) 1. If need to discover some path with templates then use
WithTemplatesFromPath
var moduleTemplatesPath = Path.Combine(ModuleInfo.FullPhysicalPath, "Templates"); registrar.RegisterNotification<SampleEmailNotification>().WithTemplatesFromPath(Path.Combine(moduleTemplatesPath, "Custom"), Path.Combine(moduleTemplatesPath, "Default"));
- use construction:
How to send notification from code
- After registration the notification need to call two services:
INotificationSearchService
andINotificationSender
- There is a service 'SampleService' for example
- Need to add the services to constructor
public class SampleService { private readonly INotificationSearchService _notificationSearchService; private readonly INotificationSender _notificationSender; public SampleService(INotificationSender notificationSender, INotificationSearchService notificationSearchService) { _notificationSender = notificationSender; _notificationSearchService = notificationSearchService; } }
- Then need to get the notification via
INotificationSearchService
in needed methodvar notification = await _notificationSearchService.GetNotificationAsync<SampleEmailNotification>();
- Then set all notification parameters for the notification like this
notification.LanguageCode = 'en-US'; notification.SetFromToMembers("[email protected]", "[email protected]");
- Then to send the notification instantly
or schedule
await _notificationSender.SendNotificationAsync(notification);
_notificationSender.ScheduleSendNotification(notification);
NOTE: look at demo-code
How to extend an exist notification type and template
- If would like to extend an exist notification then create a extend notification based on a derived notification
look at code
public class ExtendedSampleEmailNotification : SampleEmailNotification { public ExtendedSampleEmailNotification() : base(nameof(ExtendedSampleEmailNotification)) { } }
- Then need to override the notification type via
INotificationRegistrar
look at coderegistrar.OverrideNotificationType<SampleEmailNotification, ExtendedSampleEmailNotification>();
- Also there is a possibility to add templates for the extended notification
look at code
registrar.OverrideNotificationType<SampleEmailNotification, ExtendedSampleEmailNotification>().WithTemplates(new EmailNotificationTemplate() { Subject = "Extended SampleEmailNotification subject", Body = "Extended SampleEmailNotification body test" });
- And need to define derived notifications where has own types and convert the types to based type (like as SampleEmailNotification). It can be define with the Migration.
- Need to create a clean migration in project.
- Then to add SQL-script which will be update notifications.
Look at example
NOTE: Look at all samples in project
Last update: March 7, 2021