Skip to main content
A newer version of this page is available. .

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

  • 6 minutes to read

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

Note

  • This example is for the Entity Framework data model. If you use XPO, refer to the Notifications | Custom Entity With Notifications example in the Feature Center demo. The Feature Center demo is installed in %PUBLIC%\Documents\DevExpress Demos 20.2\Components.NET Core Desktop Libraries\eXpressApp Framework\FeatureCenter by default. The ASP.NET Web Forms version of this demo is available online at https://demos.devexpress.com/XAF/FeatureCenter/.
  • 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 System.Runtime.CompilerServices;
using DevExpress.Persistent.Base;
// ...
[DefaultClassOptions]
 public class Task : INotifyPropertyChanged {
     private int id;
    [Browsable(false)]
    public int Id { get; private set; }

    private string subject;
    public string Subject { 
        get { return subject; } 
        set { SetProperty<string>(ref subject, value); } 
    }

    private DateTime dueDate;
    public DateTime DueDate { 
        get { return dueDate; }
        set { SetProperty<DateTime>(ref dueDate, value);}
    }

    protected void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null) {
        if(!EqualityComparer<T>.Default.Equals(field, value)) {
            field = value;
            OnPropertyChanged(propertyName);
        }
    }
    #region INotifyPropertyChanged members
    protected void OnPropertyChanged(string propertyName) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
}

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;
    // ...
    [DefaultClassOptions]
    public class Task : INotifyPropertyChanged, ISupportNotifications {
        // ...
    
        #region ISupportNotifications members
        private DateTime? alarmTime;
        [Browsable(false)]
        public DateTime? AlarmTime {
            get { return alarmTime; }
            set {
                SetProperty<DateTime?>(ref alarmTime, value);
                if (value == null) {
                    RemindIn = null;
                    IsPostponed = false;
                }
            }
        }
        private bool isPostponed;
        [Browsable(false)]
        public bool IsPostponed {
            get { return isPostponed;}
            set { SetProperty<bool>(ref isPostponed, value);}
        }
    
        [Browsable(false), NotMapped]
        public string NotificationMessage {
            get { return Subject; }
        }
    
        private TimeSpan? remindIn;
        public TimeSpan? RemindIn {
            get { return remindIn; }
            set { SetProperty<TimeSpan?>(ref remindIn, value); }
        }
    
        [Browsable(false), NotMapped]
        public object UniqueId {
            get { return Id; }
        }
        #endregion
    }
    
  3. Implement IXafEntityObject to provide the logic that initializes the AlarmTime according to the RemindIn interval specified by the user. Here is an example logic implemented in the OnSaving method.

    using DevExpress.ExpressApp;
    // ...
    [DefaultClassOptions]
    public class Task : INotifyPropertyChanged, ISupportNotifications, IXafEntityObject {
        // ...
        #region IXafEntityObject members
        public void OnCreated() { }
        public void OnLoaded() { }
        public 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
    }
    

Run the WinForms or ASP.NET 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