Back to Devexpress

How to: Show Notifications to a Specific User

expressappframework-113696-event-planning-and-notifications-how-to-show-notifications-to-a-specific-user.md

latest8.7 KB
Original Source

How to: Show Notifications to a Specific User

  • Aug 01, 2024
  • 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).

csharp
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.
csharp
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Base.General;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;
using System.Collections.ObjectModel;

namespace MySolution.Module.BusinessObjects;
[DefaultClassOptions]
public class MyTask : BaseObject, ISupportNotifications {
    // ...
    [Association("AssignedTo-MyTasks")]
    public ApplicationUser AssignedTo {
        get {
            return GetPropertyValue<ApplicationUser>(nameof(AssignedTo));
        }
        set {
            SetPropertyValue(nameof(AssignedTo), value);
        }
    }
}

The ApplicationUser is a custom Security System user type that has the One-to-Many relationship with Tasks.

csharp
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.
csharp
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Base.General;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;
using System.Collections.ObjectModel;

namespace MySolution.Module.BusinessObjects;
[DefaultClassOptions, DefaultProperty(nameof(UserName))]
public class ApplicationUser : PermissionPolicyUser {
    // ...
    [Association("AssignedTo-MyTasks")]
    public XPCollection<MyTask> MyTasks { get { return GetCollection<MyTask>(nameof(MyTasks)); } }
}

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.

csharp
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());
        }
    }
}
csharp
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.Oid == (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.

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

In ASP.NET Core Blazor applications, use SchedulerBlazorModule:

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