The following steps need to be taken to create a custom notification gateway class:
1. | Create a new library (assembly) as a part of your solution and a new class inside this library. |
2. | Set your class to inherit from the CMS.Notifications.CMSNotificationGateway abstract class. |
3. | There are two methods you will need to override to reach the required functionality: |
• | void SendNotification() – sends a single notification; it is automatically called after the specified event is raised |
• | CMSNotificationGatewayForm GetNotificationGatewayForm() – loads and returns notification gateway form for the notification gateway |
4. | Compile the library. |
5. | Ensure the library file (*.dll) is included in the /Bin directory. |
Example - E-mail notification gateway class
The following code sample shows how a custom e-mail notification gateway class can be implemented:
EmailNotificationGateway.cs
using System; using System.Collections.Generic; using System.Text; using System.Web.UI;
using CMS.EventLog; using CMS.EmailEngine; using CMS.GlobalHelper; using CMS.SiteProvider; using CMS.SettingsProvider; using CMS.Notifications;
namespace EmailNotification { /// <summary> /// Base class for e-mail notification gateway. /// </summary> public class EmailNotificationGateway : CMSNotificationGateway { /// <summary> /// Returns the e-mail gateway form. /// </summary> public override CMSNotificationGatewayForm GetNotificationGatewayForm() { try { Control ctrl = this.NotificationSubscriptionControl.Page.LoadControl("~/CMSModules/Notifications/Controls/NotificationSubscription/EmailNotificationForm.ascx"); ctrl.ID = ValidationHelper.GetIdentificator("notif" + this.NotificationGatewayObj.GatewayName);
return (CMSNotificationGatewayForm)ctrl; } catch (Exception ex) { try { // Log the event EventLogProvider eventLog = new EventLogProvider(); eventLog.LogEvent("EmailGateway", "EXCEPTION", ex); } catch { // Unable to log the event } } return null; }
/// <summary> /// Sends the notification via e-mail. /// </summary> public override void SendNotification() { try { if (this.NotificationSubscriptionObj != null) { // Get template text NotificationTemplateTextInfo templateText = NotificationTemplateTextInfoProvider.GetNotificationTemplateTextInfo(this.NotificationGatewayObj.GatewayID, this.NotificationSubscriptionObj.SubscriptionTemplateID); if (templateText != null) { // Get the site name string siteName = null; SiteInfo si = SiteInfoProvider.GetSiteInfo(this.NotificationSubscriptionObj.SubscriptionSiteID); if (si != null) { siteName = si.SiteName; }
// Create message object EmailMessage message = new EmailMessage();
// Get sender from settings message.From = SettingsKeyProvider.GetStringValue(siteName + ".CMSSendEmailNotificationsFrom");
// Do not send the e-mail if there is no sender specified if (message.From != "") { // Initialize message message.Recipients = this.NotificationSubscriptionObj.SubscriptionTarget;
// Body if ((this.NotificationSubscriptionObj.SubscriptionUseHTML) && (this.NotificationGatewayObj.GatewaySupportsHTMLText) && (templateText.TemplateHTMLText != "")) { // HTML format, set Body property, resolve macros if possible message.EmailFormat = EmailFormatEnum.Html; if (this.Resolver != null) { message.Body = this.Resolver.ResolveMacros(templateText.TemplateHTMLText); } else { message.Body = templateText.TemplateHTMLText; } } else { // Plaintext format, set PlainTextBody property, resolve macros if possible message.EmailFormat = EmailFormatEnum.PlainText; if (this.Resolver != null) { message.PlainTextBody = this.Resolver.ResolveMacros(templateText.TemplatePlainText); } else { message.PlainTextBody = templateText.TemplatePlainText; } }
// Subject, resolve macros if possible if (this.Resolver != null) { message.Subject = this.Resolver.ResolveMacros(templateText.TemplateSubject); } else { message.Subject = templateText.TemplateSubject; }
// Send email via Email engine API EmailSender.SendEmail(siteName, message); } } } } catch (Exception ex) { try { // Log the event EventLogProvider eventLog = new EventLogProvider(); eventLog.LogEvent("EmailGateway", "EXCEPTION", ex); } catch { // Unable to log the event } } } } } |
Page url: http://devnet.kentico.com/docs/devguide/index.html?custom_notification_gateway_class.htm