Skip to main content

How to: Show Notifications to a Specific User

  • 2 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 : BaseObject, ISupportNotifications {
    // ...
    public virtual ApplicationUser AssignedTo { get; set; }
}

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.

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 System.Collections.ObjectModel;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF.PermissionPolicy;
// ...
[DefaultClassOptions, DefaultProperty(nameof(UserName))]
public class ApplicationUser : PermissionPolicyUser {
    // ...
    public virtual IList<Task> Tasks { get; set; } = new ObservableCollection<Task>();
}

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.

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;
}