Skip to main content

How to: Use Notifications with a Custom Business Class (Implement ISupportNotifications)

  • 3 minutes to read

This example demonstrates how to associate Notifications with the custom business class.

Note

  • This example is for the Entity Framework Core data model. If you use XPO, refer to the Notifications | Custom Entity With Notifications example in the Feature Center demo. The default location of the application is %PUBLIC%\Documents\DevExpress Demos 23.2\Components\XAF\FeatureCenter.NETFramework.XPO.
  • ASP.NET Core Blazor applications do not support the Notifications Module.

Assume you have the following Task business class.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
// ...
[DefaultClassOptions]
 public class Task : BaseObject {
    public virtual string Subject { get; set; }
    public virtual DateTime DueDate { get; set; }
}

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.

The goal is to notify a user before the DueDate moment arrives using functionality provided by the Notifications Module. Follow the steps below:

  1. Add this module’s components to your projects using the Module Designer:

    • Add the NotificationsModule to your Module project.

      HowTo_Reminders_0

    • Add the NotificationsModuleWeb to your Module.Web project.

      HowTo_Reminders_1

    • Add the NotificationsModuleWin to your Module.Win project.

      HowTo_Reminders_2

    Note

    By default, the notification refresh interval is 5 minutes. For testing purposes, you may reduce this interval. Double-click the WinApplication.cs(vb) file from the Win application project, select the NotificationsModule in the Modules section of the Module Designer. In the Properties window, set the NotificationsModule.NotificationsRefreshInterval to 10 seconds. Do the same for the WebApplication.cs(vb) file from the Web application project.

  2. Implement the ISupportNotifications interface in the Task class.

    using DevExpress.Persistent.Base.General;
    using DevExpress.Persistent.BaseImpl.EF;
    // ...
    [DefaultClassOptions]
    public class Task : BaseObject, ISupportNotifications {
        // ...
    
        #region ISupportNotifications members
            private DateTime? alarmTime;
        [Browsable(false)]
        public virtual DateTime? AlarmTime { 
            get { return alarmTime; }
            set {
                if (value == null) {
                    RemindIn = null;
                    IsPostponed = false;
                }
            }
            }
    
        [Browsable(false)]
        public virtual bool IsPostponed { get; set; }
    
        [Browsable(false),NotMapped]
        public string NotificationMessage {
            get { return Subject; }
        }
        public virtual TimeSpan? RemindIn { get; set; }
    
        [Browsable(false),NotMapped]
        public object UniqueId {
            get { return ID; }
        }
        #endregion
    }
    
    // Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
    
  3. Use the IXafEntityObject interface API to initialize the AlarmTime based on the RemindIn interval specified by a user.

    using DevExpress.ExpressApp;
    // ...
    [DefaultClassOptions]
    public class Task : BaseObject, ISupportNotifications {
        // ...
        #region IXafEntityObject members
        public override void OnSaving() {
            if(RemindIn.HasValue) {
                if((AlarmTime == null) || (AlarmTime < DueDate - RemindIn.Value)) {
                    AlarmTime = DueDate - RemindIn.Value;
                }
            }
            else {
                AlarmTime = null;
            }
            if (AlarmTime == null) {
                RemindIn = null;
                IsPostponed = false;
            }
        }
        #endregion
    }
    
    // Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
    

Run the WinForms or ASP.NET Web Forms application and create a new overdue Task in the past (the DueDate should be earlier than the current time). Specify a non-empty value for the RemindIn property that specifies the amount of time between the notification alert and the DueDate moment (empty RemindIn value means that the alert is never displayed).

Notifications_CustomClass

Save the task and you will see the Reminder window after the time span specified via the NotificationsModule.NotificationsRefreshInterval property or less.

HowTo_Reminders_5

See Also