Skip to main content
.NET 6.0+

How to: Show Notifications to a Specific User

  • 3 minutes to read

By default, XAF displays notifications for all users. This example demonstrates how to filter notifications in order to display the Notifications window only to a specific user.

Assume you have an ISupportNotifications business object that exposes the AssignedTo property.

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace MySolution.Module.BusinessObjects;
[DefaultClassOptions]
public class MyTask : BaseObject, ISupportNotifications {
    // ...
    public virtual ApplicationUser AssignedTo { get; set; }
}

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

Consider the Task business class implemented in the How to: Use Notifications with a Custom Business Class (Implement ISupportNotifications) example. The ApplicationUser is a custom Security System user type that has the One-to-Many relationship with Tasks.

using System.ComponentModel;
using System.Collections.ObjectModel;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF.PermissionPolicy;

namespace MySolution.Module.BusinessObjects;
[DefaultClassOptions, DefaultProperty(nameof(UserName))]
public class ApplicationUser : PermissionPolicyUser {
    // ...
    public virtual IList<MyTask> MyTasks { get; set; } = new ObservableCollection<MyTask>();
}

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

Navigate to the YourApplicationName.Module\Module.cs 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;

namespace MySolution.Module;
public sealed class MainDemoModule : ModuleBase {
    // ...
    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(MyTask)) {
            e.Criteria = CriteriaOperator.FromLambda(x => x.AssignedTo == null || x.AssignedTo.ID == (Guid)CurrentUserIdOperator.CurrentUserId());
        }
    }
}

As a result, XAF displays a notification only if the AssignedTo property’s value is empty or refers to the current user.

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

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