NotificationService Guide
A service-based notification system for Blazor applications that provides centralized notification management with support for various emphasis levels, feedback types, custom icons, and interactive actions.
Key Features
- Service-based Architecture: Centralized notification management via dependency injection
- Position Control: Configurable notification container positioning
- Rich Notification Options: Support for headings, icons, actions, emphasis levels, and feedback types
- Interactive Actions: Both callback-based actions and custom Razor component slots
- Queue Management: Built-in notification limits and automatic cleanup
- Event-driven: Reactive updates via NotificationsChanged events
Basic Setup
- Register the service in your Program.cs:
// Program.cs
using NovaComponents.Services;
var builder = WebApplication.CreateBuilder(args);
// Add Nova notification services
builder.Services.AddNovaNotifications();
// Or with custom configuration:
builder.Services.AddNovaNotifications(options => {
options.Position = NvNotificationcontainer.PositionEnum.topleft;
options.MaxNotifications = 3;
options.ClassName = "custom-notifications";
});
var app = builder.Build();
- Add the container to your MainLayout.razor:
// MainLayout.razor
<div class="page">
<main>
@Body
</main>
</div>
<NovaComponents.Services.NovaNotificationContainer />
- Use the service in your components:
@page "/example"
@inject INotificationService NotificationService
@implements IDisposable
<NvButton @onclick="ShowSuccessNotification">
Show Success
</NvButton>
<NvButton @onclick="ShowConfirmDialog">
Show Confirmation
</NvButton>
@code {
protected override void OnInitialized()
{
// Subscribe to notification changes for reactive UI updates
NotificationService.NotificationsChanged += StateHasChanged;
}
private void ShowSuccessNotification()
{
NotificationService.Show(new NotificationOptions
{
Heading = "Success!",
Message = "Your operation completed successfully",
Feedback = NvNotification.FeedbackEnum.success,
Icon = "check-circle"
});
}
private void ShowConfirmDialog()
{
var notificationId = Guid.NewGuid().ToString();
NotificationService.Show(new NotificationOptions
{
Id = notificationId,
Heading = "Confirm Action",
Message = "Are you sure you want to proceed?",
Feedback = NvNotification.FeedbackEnum.warning,
Dismissible = false,
Actions = new[] {
new NotificationAction {
Label = "Cancel",
OnClick = () => NotificationService.Dismiss(notificationId)
},
new NotificationAction {
Label = "Confirm",
OnClick = () => {
// Perform action
PerformAction();
NotificationService.Dismiss(notificationId);
}
}
}
});
}
private void PerformAction()
{
// Your business logic here
NotificationService.Show(new NotificationOptions
{
Message = "Action completed successfully!",
Feedback = NvNotification.FeedbackEnum.success
});
}
public void Dispose()
{
NotificationService.NotificationsChanged -= StateHasChanged;
}
}
Using Actions
You can add interactive buttons to notifications using either the Actions property for simple callbacks or ActionSlot for custom Razor components:
Action Callbacks
Use the Actions property to add simple button actions:
private void ShowWithActions()
{
var notificationId = Guid.NewGuid().ToString();
NotificationService.Show(new NotificationOptions
{
Id = notificationId,
Heading = "Delete File",
Message = "Are you sure you want to delete this file?",
Feedback = NvNotification.FeedbackEnum.warning,
Dismissible = false,
Actions = new[] {
new NotificationAction {
Label = "Cancel",
OnClick = () => NotificationService.Dismiss(notificationId)
},
new NotificationAction {
Label = "Delete",
OnClick = () => {
// Perform delete operation
DeleteFile();
NotificationService.Dismiss(notificationId);
// Show confirmation
NotificationService.Show(new NotificationOptions
{
Message = "File deleted successfully",
Feedback = NvNotification.FeedbackEnum.success
});
}
}
}
});
}
Custom Action Slot
Use the ActionSlot property for more complex custom layouts with full control over button styling and layout:
private void ShowWithActionSlot()
{
var notificationId = Guid.NewGuid().ToString();
NotificationService.Show(new NotificationOptions
{
Id = notificationId,
Heading = "Update Available",
Message = "A new version is available. Update now?",
Feedback = NvNotification.FeedbackEnum.information,
Dismissible = false,
ActionSlot = builder =>
{
int seq = 0;
// Custom button with specific styling
builder.OpenComponent<NvButton>(seq++);
builder.AddAttribute(seq++, "Size", NvButton.SizeEnum.md);
builder.AddAttribute(seq++, "Emphasis", NvButton.EmphasisEnum.low);
builder.AddAttribute(seq++, "onclick",
EventCallback.Factory.Create(this, () => {
NotificationService.Dismiss(notificationId);
}));
builder.AddAttribute(seq++, "ChildContent", (RenderFragment)(childBuilder =>
{
childBuilder.AddContent(0, "Update Later");
}));
builder.CloseComponent();
// Add spacing
builder.AddMarkupContent(seq++, " ");
// Primary action button
builder.OpenComponent<NvButton>(seq++);
builder.AddAttribute(seq++, "Size", NvButton.SizeEnum.md);
builder.AddAttribute(seq++, "onclick",
EventCallback.Factory.Create(this, () => {
StartUpdate();
NotificationService.Dismiss(notificationId);
}));
builder.AddAttribute(seq++, "ChildContent", (RenderFragment)(childBuilder =>
{
childBuilder.AddContent(0, "Update Now");
}));
builder.CloseComponent();
}
});
}
NotificationOptions Interface
When calling NotificationService.Show(options), you can pass the following options:
- Id (string?, optional): Unique identifier for the notification
- Heading (string?, optional): Short, concise heading text
- Message (string?, required): Main notification content
- Dismissible (bool, default: true): Whether users can manually close the notification
- Emphasis (EmphasisEnum, default: medium): Visual prominence level (low, medium, high)
- Feedback (FeedbackEnum, default: information): Color scheme and default icon (information, success, warning, error)
- Icon (string?, optional): Custom icon name to override the default
- Actions (NotificationAction[]?, optional): Array of action buttons with labels and callbacks
- ActionSlot (RenderFragment?, optional): Custom Razor component for complex action layouts
- Duration (int, default: 0): Duration in milliseconds before auto-dismissing. Set to 0 for sticky notifications that don't auto-dismiss
Auto-dismiss Notifications
Control notification lifetime using the Duration option. Notifications can either be sticky (remain until manually dismissed) or auto-dismiss after a specified time.
Sticky Notifications (Default)
By default, notifications are sticky and remain visible until manually dismissed:
// Duration defaults to 0 (sticky)
NotificationService.Show(new NotificationOptions
{
Message = "This notification stays until dismissed",
Feedback = NvNotification.FeedbackEnum.information
});
Auto-dismiss Notifications
Set a duration in milliseconds to automatically dismiss notifications:
// Auto-dismiss after 5 seconds
NotificationService.Show(new NotificationOptions
{
Heading = "Upload Complete",
Message = "Your file has been uploaded",
Feedback = NvNotification.FeedbackEnum.success,
Duration = 5000
});
When to Use Each Pattern
- Sticky (Duration: 0): Use for important notifications requiring user acknowledgment, errors, or confirmations
- Auto-dismiss (Duration > 0): Use for success messages, status updates, or informational notices that don't require action
Note: If a user manually dismisses an auto-dismiss notification, the timer is automatically cleared to prevent issues.
INotificationService API
The service provides the following methods and properties:
- Show(NotificationOptions options): Display a new notification, returns the notification ID
- Dismiss(string id): Animate out and remove a specific notification (also clears auto-dismiss timer if present)
- Remove(string id): Immediately remove a notification without animation (also clears auto-dismiss timer)
- RemoveAll(): Clear all notifications immediately (also clears all auto-dismiss timers)
- Notifications: IReadOnlyList of currently active notifications
- NotificationsChanged: Event fired when notifications are added or removed
Notifications inside dialogs
Notifications shown through the service stay above open dialogs and drawers. If you render NvNotificationcontainer yourself instead, set FollowDialogs on it; without it the container stays where you render it. Render the container alone in a stable wrapper element, and add, remove or re-key the wrapper, never the container. A wrapper removed while a dialog is open leaves the container in that dialog until the dialog closes.
Best Practices
- Always implement IDisposable and unsubscribe from NotificationsChanged in component cleanup
- Use appropriate feedback types (success, error, warning, information) for semantic meaning
- Keep notification messages concise and actionable
- Use dismissible: false sparingly, only for critical notifications requiring user action
- Consider using emphasis: 'high' for important system messages
- Provide meaningful action labels that clearly indicate what will happen
- For complex workflows, chain notifications to guide users through multi-step processes