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

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

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T177793.

Note

Assume you have the following Task business class.

[DefaultClassOptions]
 public class Task {
    [Browsable(false)]
    public int Id { get; private set; }
    public string Subject { get; set; }
    public DateTime DueDate { get; set; }
}

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.

    [DefaultClassOptions]
    public class Task : ISupportNotifications {
        // ...
    
        #region ISupportNotifications members
        private DateTime? alarmTime;
        [Browsable(false)]
        public DateTime? AlarmTime {
            get { return alarmTime; }
            set {
                alarmTime = value;
                if (value == null) {
                    RemindIn = null;
                    IsPostponed = false;
                }
            }
        }
        [Browsable(false)]
        public bool IsPostponed { get; set; }
        [Browsable(false), NotMapped]
        public string NotificationMessage {
            get { return Subject; }
        }
        public TimeSpan? RemindIn { get; set; }
        [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.

    [DefaultClassOptions]
    public class Task : 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