![]() |
VOOZH | about |
dotnet add package benxu.AppPlatform.Notification --version 3.2.7
NuGet\Install-Package benxu.AppPlatform.Notification -Version 3.2.7
<PackageReference Include="benxu.AppPlatform.Notification" Version="3.2.7" />
<PackageVersion Include="benxu.AppPlatform.Notification" Version="3.2.7" />Directory.Packages.props
<PackageReference Include="benxu.AppPlatform.Notification" />Project file
paket add benxu.AppPlatform.Notification --version 3.2.7
#r "nuget: benxu.AppPlatform.Notification, 3.2.7"
#:package benxu.AppPlatform.Notification@3.2.7
#addin nuget:?package=benxu.AppPlatform.Notification&version=3.2.7Install as a Cake Addin
#tool nuget:?package=benxu.AppPlatform.Notification&version=3.2.7Install as a Cake Tool
通知歷史管理服務 - 自動儲存與管理推播通知歷史記錄,提供已讀/未讀標記、分類、導航等功能。
安裝套件:
dotnet add package benxu.AppPlatform.Notification
確認已安裝相依套件:
benxu.AppPlatform.Corebenxu.AppPlatform.Storage.LiteDBbenxu.AppPlatform.Firebase.Fcm在 MauiProgram.cs 中註冊服務:
using benxu.AppPlatform.Notification.Extensions;
builder.Services.AddNotificationHistory();
注意:必須在註冊 FCM 服務之後註冊,因為通知歷史服務需要訂閱 FCM 事件。
完整範例:
builder.UseAppPlatform(options =>
{
options.UseFirebaseFirestore(firestore => { /* ... */ });
options.UseLocalStorage(storage => { /* ... */ });
options.UseFcm(fcm => { /* ... */ });
});
// 在 AppPlatform 初始化後註冊
builder.Services.AddNotificationHistory();
@inject INotificationHistoryService NotificationHistory
@implements IDisposable
<h3>通知中心</h3>
@if (notifications != null)
{
foreach (var notification in notifications)
{
<div class="notification-item @(notification.IsRead ? "read" : "unread")">
<h4>@notification.Title</h4>
<p>@notification.Body</p>
<small>@notification.ReceivedAt.ToLocalTime()</small>
@if (!notification.IsRead)
{
<button @onclick="() => MarkAsRead(notification.Id)">
標記為已讀
</button>
}
@if (!string.IsNullOrEmpty(notification.NavigationUrl))
{
<button @onclick="() => NavigateTo(notification.NavigationUrl)">
查看詳情
</button>
}
</div>
}
}
<div class="actions">
<button @onclick="ClearAll">清除所有通知</button>
<p>未讀數量:@unreadCount</p>
</div>
@code {
private IEnumerable<NotificationHistory>? notifications;
private int unreadCount;
protected override async Task OnInitializedAsync()
{
// 訂閱歷史變更事件
NotificationHistory.HistoryChanged += OnHistoryChanged;
await LoadNotifications();
}
private async Task LoadNotifications()
{
var result = await NotificationHistory.GetAllNotificationsAsync();
if (result.IsSuccess)
{
notifications = result.Data;
}
var countResult = await NotificationHistory.GetUnreadCountAsync();
if (countResult.IsSuccess)
{
unreadCount = countResult.Data;
}
}
private async Task MarkAsRead(string notificationId)
{
await NotificationHistory.MarkAsReadAsync(notificationId);
}
private async Task ClearAll()
{
await NotificationHistory.ClearAllNotificationsAsync();
}
private void OnHistoryChanged(object? sender, EventArgs e)
{
// 重新載入通知
InvokeAsync(LoadNotifications);
StateHasChanged();
}
public void Dispose()
{
NotificationHistory.HistoryChanged -= OnHistoryChanged;
}
}
// 注入服務
private readonly INotificationHistoryService _notificationHistory;
// 取得所有通知
var result = await _notificationHistory.GetAllNotificationsAsync();
if (result.IsSuccess)
{
var notifications = result.Data;
foreach (var notification in notifications)
{
Console.WriteLine($"{notification.Title}: {notification.Body}");
}
}
// 取得未讀數量
var countResult = await _notificationHistory.GetUnreadCountAsync();
if (countResult.IsSuccess)
{
Console.WriteLine($"未讀通知:{countResult.Data} 則");
}
// 標記為已讀
await _notificationHistory.MarkAsReadAsync(notificationId);
// 刪除通知
await _notificationHistory.DeleteNotificationAsync(notificationId);
// 清除所有通知
await _notificationHistory.ClearAllNotificationsAsync();
| 方法 | 說明 | 參數 | 回傳值 |
|---|---|---|---|
AddNotificationAsync |
新增通知歷史 | NotificationHistory notification, CancellationToken cancellationToken = default |
Task<Result> |
GetAllNotificationsAsync |
取得所有通知(按時間降序) | CancellationToken cancellationToken = default |
Task<Result<IEnumerable<NotificationHistory>>> |
DeleteNotificationAsync |
刪除指定通知 | string notificationId, CancellationToken cancellationToken = default |
Task<Result> |
ClearAllNotificationsAsync |
清除所有通知 | CancellationToken cancellationToken = default |
Task<Result> |
MarkAsReadAsync |
標記通知為已讀 | string notificationId, CancellationToken cancellationToken = default |
Task<Result> |
GetUnreadCountAsync |
取得未讀通知數量 | CancellationToken cancellationToken = default |
Task<Result<int>> |
| 事件 | 說明 | 類型 |
|---|---|---|
HistoryChanged |
通知歷史變更時觸發(新增/刪除/標記已讀) | EventHandler? |
| 屬性 | 類型 | 說明 |
|---|---|---|
Id |
string |
通知 ID(主鍵) |
Title |
string |
通知標題 |
Body |
string |
通知內容 |
ReceivedAt |
DateTime |
接收時間(UTC) |
NavigationUrl |
string? |
導航路由(例如:/todos/detail?id=123) |
IsRead |
bool |
是否已讀 |
SourceNotificationId |
string? |
原始 FCM 通知 ID |
Data |
Dictionary<string, string>? |
自訂資料 |
Category |
string? |
通知類型/分類(例如:todo、system、reminder) |
服務會自動訂閱以下 FCM 事件:
NotificationReceived(前景接收)
NotificationTapped(背景/終止狀態點擊)
支援自動將 FCM Deep Link 轉換為 Blazor 路由:
todoapp://todos/detail?id=123/todos/detail?id=123可透過 NavigationUrl 屬性取得轉換後的路由進行導航。
HistoryChanged 事件benxu.AppPlatform.Corebenxu.AppPlatform.Storage.LiteDBbenxu.AppPlatform.Firebase.FcmMIT License - Copyright (c) 2025 benxu
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-android36.0 net10.0-android36.0 is compatible. net10.0-ios26.0 net10.0-ios26.0 is compatible. |
Showing the top 1 NuGet packages that depend on benxu.AppPlatform.Notification:
| Package | Downloads |
|---|---|
|
benxu.AppPlatform.MAUI.Bootstrap
Bootstrap package for benxu App Platform. Provides fluent API for one-line service registration and lifecycle management. |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.2.7 | 91 | 6/5/2026 |
| 3.2.6 | 116 | 5/30/2026 |
| 3.2.5 | 111 | 4/25/2026 |
| 3.2.4 | 96 | 4/25/2026 |
| 3.2.3 | 107 | 4/23/2026 |
| 3.2.2 | 111 | 4/22/2026 |
| 3.2.1 | 113 | 4/17/2026 |
| 3.2.0 | 107 | 4/11/2026 |
| 3.1.9 | 109 | 4/10/2026 |
| 3.1.8 | 106 | 4/10/2026 |
| 3.1.7 | 106 | 4/10/2026 |
| 3.1.6 | 103 | 4/10/2026 |
| 3.1.5 | 102 | 4/9/2026 |
| 3.1.4 | 107 | 4/7/2026 |
| 3.1.3 | 112 | 3/24/2026 |
| 3.1.2 | 108 | 3/23/2026 |
| 3.1.1 | 107 | 3/22/2026 |
| 3.1.0 | 107 | 3/22/2026 |
| 3.0.9 | 115 | 3/20/2026 |
| 3.0.8 | 111 | 3/19/2026 |