Orchardcore notifications

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore notifications is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for Orchard Core notifications. Covers the Notifications, Email and SMS Notifications features, the bell and Notification Center, sending messages with INotificationService and NotificationSendResult, notification types with AddNotificationType, per-type preferences and DisabledMethods, URLs a

How to use this skill

Skill file

Orchard Core Notifications

You are an Orchard Core expert. Send persistent notifications to users, give each module its own notification types, and keep the notification center in sync with the records notifications are about.

Guidelines

Features

| Feature | ID | Depends on |

|---|---|---|

| Notifications | OrchardCore.Notifications | OrchardCore.Liquid |

| Email Notifications | OrchardCore.Notifications.Email | OrchardCore.Notifications, OrchardCore.Email |

| SMS Notifications (declared in the OrchardCore.Sms module) | OrchardCore.Notifications.Sms | OrchardCore.Notifications, OrchardCore.Sms |

{
  "steps": [
    {
      "name": "Feature",
      "enable": [
        "OrchardCore.Notifications",
        "OrchardCore.Notifications.Email"
      ]
    }
  ]
}

The email method (Method = Email) needs a configured email provider and sends Subject with HtmlBody when IsHtmlPreferred and an HTML body exist, otherwise TextBody; it fails for a user without an email address. The SMS method (Method = SMS) needs an SMS provider and sends TextBody to the user's PhoneNumber, failing when it is empty.

Where Users See Notifications

ManageNotifications is granted by default to Administrator, Editor, Moderator, Author, Contributor and Authenticated, so every signed-in user has a bell.

Configuration

NotificationOptions binds OrchardCore:Notifications (the deprecated OrchardCore_Notifications still binds):

{
  "OrchardCore": {
    "Notifications": {
      "TotalUnreadNotifications": 10,
      "MaxEvaluatedUnreadNotifications": 100,
      "DisableNotificationHtmlBodySanitizer": false,
      "AbsoluteCacheExpirationSeconds": 0,
      "SlidingCacheExpirationSeconds": 1800
    }
  }
}

| Option | Default | Purpose |

|---|---|---|

| TotalUnreadNotifications | 10 | Unread notifications shown in the bell menu. |

| MaxEvaluatedUnreadNotifications | 100 | With a visibility handler registered, the most recent unread notifications evaluated to count the badge; more shows as 100+. |

| DisableNotificationHtmlBodySanitizer | false | Skips sanitizing the HTML Body of the notify workflow tasks when they are saved. |

| AbsoluteCacheExpirationSeconds | 0 | Absolute lifetime of the cached bell menu; 0 means none. |

| SlidingCacheExpirationSeconds | 1800 | Sliding lifetime of the cached bell menu; 0 disables it. |

The bell menu is cached per user (tags top-unread-user-notifications and per-user keys from NotificationsHelper) only when one expiration is greater than 0. INotificationManager and the built-in actions clear it; a visibility change alone shows when the entry expires.

Sending a Notification

using Microsoft.Extensions.Localization;
using OrchardCore.Notifications;
using OrchardCore.Notifications.Models;
using OrchardCore.Users.Models;

namespace MyModule.Services;

public sealed class ApprovalNotifier
{
    private readonly INotificationService _notificationService;
    private readonly IStringLocalizer S;

    public ApprovalNotifier(
        INotificationService notificationService,
        IStringLocalizer<ApprovalNotifier> stringLocalizer)
    {
        _notificationService = notificationService;
        S = stringLocalizer;
    }

    public async Task<NotificationSendResult> NotifyAuthorAsync(User author, string contentItemId, string title)
    {
        return await _notificationService.SendAsync(author, new NotificationMessage
        {
            Subject = S["Your article was approved"],
            Summary = S["'{0}' is now published.", title],
            TextBody = S["Your article '{0}' was reviewed and published.", title],
            Url = $"~/Admin/Contents/ContentItems/{contentItemId}/Edit",
            Type = "Approval",
            ReferenceId = contentItemId,
        });
    }
}

| Message property | Purpose |

|---|---|

| Subject | Required title; no HTML. Email subject. |

| Summary | Short text under the subject in the bell and the center; HTML is sanitized when shown. |

| TextBody, HtmlBody, IsHtmlPreferred | Body sent by the methods and shown on the notification page (stored as NotificationBodyInfo). |

| Url | ~/ app-relative (resolved with the tenant prefix when opened), a local / path, or an absolute http/https URL. Anything else, such as javascript:, is dropped with a warning. |

| Type | Kind of notification (indexed). |

| ReferenceId | Id of the record it is about (indexed). |

NotificationMessage is an Entity: data added with message.Put(new MyInfo { ... }) is copied to the stored Notification for display drivers and visibility handlers. Notification has NotificationId, Subject, Summary, Type, ReferenceId, Url, UserId, CreatedUtc, plus NotificationBodyInfo and NotificationReadInfo (IsRead, ReadAtUtc). Notifications are stored in the YesSql Notification collection.

NotificationSendResult has Notification, SuccessfulCount, FailedCount, Errors, and Status: Success, PartiallySuccessful, Failed (every method that ran failed), or None (no method ran, for example no method feature enabled or the user opted out of all).

To notify a role, resolve its users with UserManager<IUser>.GetUsersInRoleAsync("Editor") and call SendAsync once per user (see references/notifications-examples.md).

Notification Types

using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Localization;
using OrchardCore.Modules;

namespace MyModule;

public static class TaskNotificationConstants
{
    public const string TaskAssigned = "TaskAssigned";
}

public sealed class Startup : StartupBase
{
    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddNotificationType(
            TaskNotificationConstants.TaskAssigned,
            LocalizationSource.Create("Assigned tasks", typeof(Startup)),
            type =>
            {
                type.Description = LocalizationSource.Create("When a task is assigned to you.", typeof(Startup));

                // Off for this type until a user turns it on.
                type.DisabledMethods.Add("SMS");
            });
    }
}

AddNotificationType (namespace Microsoft.Extensions.DependencyInjection, package OrchardCore.Notifications.Abstractions) adds a NotificationTypeDescriptor (Name, DisplayName, Description, DisabledMethods, case-insensitive) to NotificationTypeOptions.Types; registering a name again replaces it. A registered type gets, with no more code:

Unregistered types still work (filter, index, queries) but show no badge or preferences row.

Managing Sent Notifications

using OrchardCore.ContentManagement.Handlers;
using OrchardCore.Notifications;

namespace MyModule.Handlers;

public sealed class TaskNotificationsContentHandler : ContentHandlerBase
{
    private readonly INotificationManager _notificationManager;

    public TaskNotificationsContentHandler(INotificationManager notificationManager)
    {
        _notificationManager = notificationManager;
    }

    public override async Task RemovedAsync(RemoveContentContext context)
    {
        if (context.ContentItem.ContentType == "Task")
        {
            await _notificationManager.DeleteAsync(new NotificationFilter
            {
                Type = TaskNotificationConstants.TaskAssigned,
                ReferenceId = context.ContentItem.ContentItemId,
            });
        }
    }
}

INotificationManager: FindByIdAsync, ListAsync(filter) (most recent first), UpdateAsync, DeleteAsync(notification), DeleteAsync(filter) (returns the count), MarkAsReadAsync(notification) / MarkAsUnreadAsync(notification) (return whether the state changed), MarkAsReadAsync(filter), and IsRead. NotificationFilter combines UserId, Type, ReferenceId, IsRead, plus Skip/Take for ListAsync; at least one of UserId, Type or ReferenceId is required (HasCriteria). Filter deletes and marks run in batches inside the request's transaction, and every change refreshes the affected users' bell menu. The module also has FindForUserAsync and FindVisibleForUserAsync extension methods that check ownership (and visibility).

Display

Notifications render through IDisplayManager<Notification>:

| Display type | Where | Zones |

|---|---|---|

| Header (NotificationConstants.DisplayTypes.Header) | Bell menu item | Content, Meta, Actions |

| SummaryAdmin | Center row, List layout | Checkbox, Content, Tags, Meta, Actions, ActionsMenu |

| SummaryAdminGrid | Center row, Grid layout | Select, Subject, Received, Actions, ActionsMenu |

| DetailAdmin | Notification page | Content, Meta, Actions |

Built-in shapes (move, hide or override them): NotificationsSubject_Header, NotificationsSubject_SummaryAdmin, NotificationsBody_DetailAdmin, NotificationsRestricted_SummaryAdmin (restricted notifications), NotificationsType_Header, NotificationsType_SummaryAdmin, NotificationsMeta_Header, NotificationsMeta_SummaryAdmin, NotificationsCheckbox_SummaryAdmin, NotificationsActions_SummaryAdmin, NotificationsButtonActions_SummaryAdmin, NotificationsActions_DetailAdmin.

Add data per type with a driver whose CanHandleModel checks the type:

using Microsoft.AspNetCore.Http;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Notifications;

namespace MyModule.Drivers;

internal sealed class TaskAssignedNotificationDisplayDriver : DisplayDriver<Notification>
{
    private readonly INotificationVisibilityEvaluator _visibilityEvaluator;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public TaskAssignedNotificationDisplayDriver(
        INotificationVisibilityEvaluator visibilityEvaluator,
        IHttpContextAccessor httpContextAccessor)
    {
        _visibilityEvaluator = visibilityEvaluator;
        _httpContextAccessor = httpContextAccessor;
    }

    public override bool CanHandleModel(Notification notification)
        => notification.Type == TaskNotificationConstants.TaskAssigned;

    public override async Task<IDisplayResult> DisplayAsync(Notification notification, BuildDisplayContext context)
    {
        // A restricted notification must not show the data of its record.
        if (!notification.TryGet<TaskAssignedInfo>(out var info) ||
            await _visibilityEvaluator.EvaluateAsync(notification, _httpContextAccessor.HttpContext?.User) == NotificationVisibility.Restricted)
        {
            return null;
        }

        return View("TaskAssignedNotificationProject", info)
            .Location(NotificationConstants.DisplayTypes.Header, "Meta:5")
            .Location(OrchardCoreConstants.DisplayType.SummaryAdmin, "Tags:10")
            .Location(OrchardCoreConstants.DisplayType.SummaryAdminGrid, "Subject:10")
            .Location(OrchardCoreConstants.DisplayType.DetailAdmin, "Meta:5");
    }
}

Register with services.AddDisplayDriver<Notification, TaskAssignedNotificationDisplayDriver>(). The bell's Meta zone is one line of small muted text separated by dots: render plain text there and keep buttons for Actions. To replace a whole template, use the alternates Notification__{Type} or Notification_{DisplayType}__{Type} (for example Notification-TaskAssigned.Header.cshtml); restricted notifications always use the default templates. Templates read Model.Notification and Model.NotificationVisibility.

Visibility Handlers

When a user can lose access to the record after the notification was sent, implement INotificationVisibilityHandler. It runs whenever notifications are shown (bell, badge count, center, opening):

using OrchardCore.Notifications;

namespace MyModule.Services;

public sealed class TaskVisibilityHandler : INotificationVisibilityHandler
{
    private readonly IProjectMembershipService _membershipService;

    public TaskVisibilityHandler(IProjectMembershipService membershipService)
    {
        _membershipService = membershipService;
    }

    public async Task EvaluateAsync(IReadOnlyList<NotificationVisibilityContext> contexts, CancellationToken cancellationToken = default)
    {
        // Every handler sees every shown notification (one user): skip other types and query once.
        var tasks = contexts.Where(context => context.Notification.Type == TaskNotificationConstants.TaskAssigned).ToList();

        if (tasks.Count == 0)
        {
            return;
        }

        var projectIds = tasks.Select(context => context.Notification.GetOrCreate<TaskAssignedInfo>().ProjectId).ToHashSet();
        var memberOf = await _membershipService.GetProjectsOfUserAsync(projectIds, contexts[0].User);

        foreach (var context in tasks)
        {
            if (!memberOf.Contains(context.Notification.GetOrCreate<TaskAssignedInfo>().ProjectId))
            {
                context.Hide();
            }
        }
    }
}

Method Providers and Events

using Microsoft.Extensions.Localization;
using OrchardCore.Infrastructure;
using OrchardCore.Notifications;

namespace MyModule.Services;

public sealed class WebhookNotificationMethodProvider : INotificationMethodProvider
{
    internal readonly IStringLocalizer S;

    public WebhookNotificationMethodProvider(IStringLocalizer<WebhookNotificationMethodProvider> stringLocalizer)
    {
        S = stringLocalizer;
    }

    // Stored in user preferences and type DisabledMethods: keep it stable.
    public string Method => "Webhook";

    public LocalizedString Name => S["Webhook Notifications"];

    public Task<Result> SendAsync(object notify, INotificationMessage message, CancellationToken cancellationToken = default)
    {
        // Deliver message.Subject / TextBody; return Result.Failed(...) on error.
        return Task.FromResult(Result.Success());
    }
}

Register with services.AddScoped<INotificationMethodProvider, WebhookNotificationMethodProvider>(), normally in its own feature so users see it only when enabled. Methods are ordered by the user's preference, then by Name.

INotificationEvents has CreatingAsync, CreatedAsync (around saving), SendingAsync/SentAsync (whole send), and per-method SendingAsync, SentAsync, FailedAsync. Derive from NotificationEventsHandler (OrchardCore.Notifications.Services) and override what you need; register with services.AddScoped<INotificationEvents, MyEvents>(). A failing handler is logged and does not stop the send.

Notification Center Admin List

The center renders with the shared AdminList shape (list name Notifications), so it follows the List/Grid and actions layout settings (see orchardcore-admin-lists). Rows carry notification-is-unread or notification-is-read (NotificationsAdminList.UnreadCssClass, ReadCssClass).

| Search term | Values |

|---|---|

| status: | Read, Unread (the Show filter: All, Read, Unread) |

| sort: | Latest (Recently created, default), Oldest (Previously created) |

| type: | A type name (the Type filter, shown when types are registered) |

| text | Bare text searches NotificationIndex.Content (summary plus text body, HTML stripped, 705 characters) |

Bulk actions (NotificationBulkAction): Mark as read, Mark as unread, Remove, plus Mark All as Read. Add terms with an INotificationAdminListFilterProvider (builds the QueryEngineBuilder<Notification>); filter on NotificationIndex.

NotificationIndex (NotificationId, UserId, IsRead, ReadAtUtc, CreatedAtUtc, Type, ReferenceId, Content) is declared .ExclusiveToDocumentType().SingleRecordPerDocument(), so counts read the index table without joining documents. Query it with collection: NotificationConstants.NotificationCollection.

Workflow Tasks

With OrchardCore.Workflows, the Notifications category adds Notify Specific Users Task (NotifyUserTask, User names comma-separated, Liquid allowed) and, with OrchardCore.Users and OrchardCore.Contents, Notify Content's Owner Task (NotifyContentOwnerTask). Both have Subject (required, Liquid), Summary, URL (Liquid), Type, Text Body, HTML Body and Use HTML Body when possible, with outcomes Done, Failed and Failed: no user found.

Permissions

| Permission | Purpose | Default roles |

|---|---|---|

| ManageNotifications (NotificationPermissions.ManageNotifications) | The bell, the center, opening and marking one's own notifications | Administrator, Editor, Moderator, Author, Contributor, Authenticated |

Troubleshooting

| Symptom | Cause |

|---|---|

| No bell | Feature disabled, or the role lacks ManageNotifications. |

| Notification sent but not in the center | The recipient was not a User, so UserId is empty. |

| No email or SMS | Method feature disabled, provider not configured, user opted out, or the type disables the method (Status is None). |

| Bell count stale | Cached menu: change notifications through INotificationManager, or lower the cache expirations. |

| Type shows as raw name | The type is not registered with AddNotificationType in an enabled feature. |

| ArgumentException on send | Type over 64 or ReferenceId over 255 characters. |

Upgrading from 3.x

See references/notifications-examples.md for role notifications, partial delivery, custom methods and events, workflow tasks, and the full custom type walkthrough.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.