Skip to main content
.NET 6.0+

How to: Show Notifications to a Specific User

  • 3 minutes to read

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

For instance, you have an ISupportNotifications business object that exposes the AssignedTo property. You can refer to the following topic for an example of the class implementation: How to: Use Notifications with a Custom Business Class (Implement ISupportNotifications).

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.

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<MyTask>(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.

Note

A user must have security permissions for the ISuppportNotifications objects in order to see them.

If you use the scheduler event descendant instead of a custom ISupportNotifications object, 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>();
    ISchedulerNotificationsProvider notificationsProvider = schedulerModule.NotificationsProvider;
    notificationsProvider.CustomizeNotificationCollectionCriteria += notificationsProvider_CustomizeNotificationCollectionCriteria;
}

In ASP.NET Core Blazor applications, use SchedulerBlazorModule:

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