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
.
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
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.
Override the
OnSaving
method to initializeAlarmTime
based on theRemindIn
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.
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 theRemindIn
property that defines the time between the notification alert and theDueDate
moment (an emptyRemindIn
value means that the alert is never displayed).Save the task. The Notifications window should appear after the time span specified in the NotificationsOptionsBase.NotificationsRefreshInterval property or sooner.