Skip to main content
.NET 8.0+

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

  • 4 minutes to read

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

Assume you have the following MyTask business class.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;

namespace MySolution.Module.BusinessObjects;
[DefaultClassOptions]
 public class MyTask : 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 use the functionality of the Notifications module to send a reminder to a user before the DueDate.

  1. Add the Notifications module to your application.

    Note

    The default notification refresh interval is 5 minutes, but you can reduce this interval for testing purposes. For more information, refer to the following topic: How to Specify the Notification Refresh Frequency

  2. Implement the ISupportNotifications interface in the MyTask class.

    using DevExpress.Persistent.Base.General;
    using DevExpress.Persistent.BaseImpl.EF;
    // ...
    [DefaultClassOptions]
    public class MyTask : 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;
                }
                alarmTime = value;
            }
        }
    
        [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. Override the OnSaving method to initialize AlarmTime based on the RemindIn interval specified by a user.

    using DevExpress.ExpressApp;
    // ...
    [DefaultClassOptions]
    public class MyTask : 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.
    
  4. Run the 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 defines the time between the notification alert and the DueDate moment (an empty RemindIn value means that the alert is never displayed).

    XAF ASP.NET Core Blazor, Notifications Enabled in Custom Class, DevExpress

  5. Save the task. The Notifications window should appear after the time span specified in the NotificationsOptionsBase.NotificationsRefreshInterval property or sooner.

    XAF ASP.NET Core Blazor, Notifications Window  in Custom Class, DevExpress

See Also