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

How to: Show Notifications to a Specific User

  • 3 minutes to read

By default, notifications are displayed to all users. This example demonstrates how to filter notifications in order to display the reminders window only to a required user.

Note

  • This example is based on the Entity Framework data model, but you can use the same approach with the eXpress Persistent Objects (XPO) as well.

  • ASP.NET Core Blazor applications do not support the Notifications Module.

Assume you have the ISupportNotifications business object exposing the AssignedTo property.

[DefaultClassOptions]
public class Task : ISupportNotifications, IXafEntityObject {
    // ...
    public virtual Employee AssignedTo { get; set; }
}

Here, we consider the Task business class implemented in the How to: Use Notifications with a Custom Business Class (Implement ISupportNotifications) example. The Employee is a custom Security System user type that is in a one-to-many association with Tasks.

using System.ComponentModel;
using DevExpress.Persistent.Base;
// ...
[DefaultClassOptions, DefaultProperty(nameof(UserName))]
public class Employee : DevExpress.Persistent.BaseImpl.EF.User {
    public Employee() {
        Tasks = new List<Task>();
    }
    public virtual IList<Task> Tasks { get; set; }
}

Edit the Module.cs (Module.vb) file. In the overridden ModuleBase.Setup method, subscribe to the XafApplication.LoggedOn event. In this event handler, get the NotificationsModule instance and subscribe to the DefaultNotificationsProvider.CustomizeNotificationCollectionCriteria event.

using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.Notifications;
using DevExpress.Persistent.Base.General;
// ...
public override void Setup(XafApplication application) {
    base.Setup(application);
    application.LoggedOn += application_LoggedOn;
}
void application_LoggedOn(object sender, LogonEventArgs e) {
   NotificationsModule notificationsModule = Application.Modules.FindModule<NotificationsModule>();
   DefaultNotificationsProvider notificationsProvider = notificationsModule.DefaultNotificationsProvider;
   notificationsProvider.CustomizeNotificationCollectionCriteria += notificationsProvider_CustomizeNotificationCollectionCriteria;
}
void notificationsProvider_CustomizeNotificationCollectionCriteria(
    object sender, CustomizeCollectionCriteriaEventArgs e) {
    if (e.Type == typeof(Task)) {
        e.Criteria = CriteriaOperator.Parse("AssignedTo is null || AssignedTo.Id == CurrentUserId()");
    }
}

As a result, a notification will be displayed only if the AssignedTo value is empty or refers to the current user.

If you use the scheduler event descendant instead of a custom ISupportNotifications object, use the SchedulerModuleBase.NotificationsProvider property to access the NotificationsProvider object and handle the NotificationsProvider.CustomizeNotificationCollectionCriteria event.

using DevExpress.ExpressApp.Scheduler;
// ...
void application_LoggedOn(object sender, LogonEventArgs e) {
    SchedulerModuleBase schedulerModule = Application.Modules.FindModule<SchedulerModuleBase>();
    NotificationsProvider notificationsProvider = schedulerModule.NotificationsProvider;
    notificationsProvider.CustomizeNotificationCollectionCriteria += notificationsProvider_CustomizeNotificationCollectionCriteria;
}