MoneyTree.Notification 通知中心
📋 概述
MoneyTree.Notification 提供统一的多渠道消息通知抽象层,屏蔽底层服务商差异。开发者通过 INotificationService 接口发送通知,支持短信、邮件、App 推送和站内信四种渠道,内置 Scriban 模板引擎渲染消息内容。
| 属性 |
说明 |
| NuGet 包 |
MoneyTree.Notification |
| 外部依赖 |
MoneyTree.Core、Scriban |
| 定位 |
多渠道消息通知抽象与分发 |
📦 包体系结构
MoneyTree.Notification ← 核心包:接口 + 本地渠道 + 模板渲染
├── MoneyTree.Notification.Sms.Aliyun ← 扩展包:阿里云短信
├── MoneyTree.Notification.Email.Smtp ← 扩展包:SMTP 邮件
└── MoneyTree.Notification.Push ← 扩展包:App 推送
🏗️ 项目文件结构
MoneyTree.Notification/
├── MoneyTree.Notification.csproj
├── GlobalUsings.cs
├── Abstractions/
│ ├── INotificationService.cs # 统一通知发送接口
│ ├── INotificationChannel.cs # 通知渠道抽象
│ └── INotificationTemplateRenderer.cs # 模板渲染接口
├── Core/
│ ├── NotificationOptions.cs # 全局配置选项
│ ├── NotificationMessage.cs # 通知消息模型
│ ├── NotificationResult.cs # 发送结果模型
│ └── NotificationRecord.cs # 通知记录(持久化)
├── Services/
│ └── NotificationService.cs # 通知服务实现
├── Channels/
│ └── LocalChannel.cs # 本地渠道(输出到日志)
├── Renderers/
│ └── ScribanTemplateRenderer.cs # Scriban 模板引擎
└── Extensions/
├── NotificationBuilder.cs # 流畅配置构造器
└── NotificationExtensions.cs # DI 注册扩展
🎯 核心能力
| 能力 |
说明 |
| 多渠道发送 |
短信、邮件、App 推送、站内信统一接口 |
| 模板引擎 |
基于 Scriban,支持变量替换和条件渲染 |
| 批量发送 |
SendBatchAsync 一次发送多条消息 |
| 结果记录 |
NotificationRecord 可用于持久化通知历史 |
| 重试策略 |
内置重试机制(可配置次数和间隔) |
| 本地开发 |
LocalChannel 输出到日志,无需外部依赖 |
🚀 快速开始
基础配置
// Program.cs
using MoneyTree.Notification.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMoneyTreeNotification(n =>
{
n.WithDefaultChannel(NotificationChannel.Local)
.WithRetry(3, 5)
.AddTemplate("order_shipped", "订单发货通知",
"您的订单 {{ order_number }} 已于 {{ shipped_at }} 发货",
channel: NotificationChannel.Sms,
titleTemplate: "订单发货通知");
});
阿里云短信
builder.Services.AddMoneyTreeNotification(n =>
{
n.UseAliyunSms(
accessKeyId: "your-ak",
accessKeySecret: "your-sk",
signName: "发财树科技")
.AddTemplate("code_verify", "验证码",
"您的验证码是 {{ code }},有效期 {{ minutes }} 分钟");
});
SMTP 邮件
builder.Services.AddMoneyTreeNotification(n =>
{
n.UseSmtpEmail(
host: "smtp.example.com",
port: 587,
userName: "noreply@example.com",
password: "your-password",
fromAddress: "noreply@example.com",
fromName: "发财树通知");
});
📖 使用示例
基础发送
public class OrderService
{
[Inject] public INotificationService Notification { get; private set; } = default!;
public async Task NotifyOrderShippedAsync(string orderNumber, string phone)
{
// 方式一:直接发送
await Notification.SendAsync(new NotificationMessage
{
Recipient = phone,
Title = "订单发货通知",
Content = $"您的订单 {orderNumber} 已发货",
Channel = NotificationChannel.Sms
});
// 方式二:使用模板发送(自动渲染)
await Notification.SendByTemplateAsync(
"order_shipped",
phone,
new
{
order_number = orderNumber,
shipped_at = DateTime.Now.ToString("yyyy-MM-dd")
});
}
}
批量发送
public async Task NotifyBatchAsync(List<string> phones, string content)
{
var messages = phones.Select(p => new NotificationMessage
{
Recipient = p,
Content = content,
Channel = NotificationChannel.Sms
});
var results = await Notification.SendBatchAsync(messages);
// results 包含每条消息的发送结果
}
发送邮件
await Notification.SendAsync(new NotificationMessage
{
Recipient = "user@example.com",
Title = "订单确认",
Content = "<h1>感谢您的订单</h1><p>您的订单已确认</p>",
Channel = NotificationChannel.Email
});
发送验证码
await notification.SendByTemplateAsync("code_verify", "13800138000",
new { code = "123456", minutes = 5 });
📋 消息模型说明
NotificationMessage
| 属性 |
类型 |
说明 |
Recipient |
string |
接收人(手机号/邮箱/设备Token) |
Title |
string? |
消息标题 |
Content |
string |
消息内容 |
Channel |
NotificationChannel |
发送渠道 |
TemplateKey |
string? |
使用的模板键 |
TemplateData |
object? |
模板渲染数据 |
NotificationResult
| 属性 |
类型 |
说明 |
IsSuccess |
bool |
是否发送成功 |
MessageId |
string? |
服务商返回的消息 ID |
ErrorMessage |
string? |
错误信息 |
Channel |
NotificationChannel |
使用的渠道 |
SentAt |
DateTime |
发送时间 |
📝 Scriban 模板语法
框架使用 Scriban 模板引擎,支持丰富的模板语法:
{# 变量替换 #}
您的订单 {{ order_number }} 已于 {{ shipped_at }} 发货
{# 条件判断 #}
{{ if is_vip }}
尊敬的 VIP 会员,您的订单已优先处理
{{ else }}
您的订单正在处理中
{{ end }}
{# 循环 #}
您购买了以下商品:
{{ for item in items }}
- {{ item.name }} x {{ item.quantity }}
{{ end }}
🔌 渠道对比
| 渠道 |
核心包 |
扩展包 |
适用场景 |
| 本地 |
✅ LocalChannel |
— |
开发/测试环境,输出到日志 |
| 短信 |
❌ |
Notification.Sms.Aliyun |
验证码、通知提醒 |
| 邮件 |
❌ |
Notification.Email.Smtp |
订单确认、营销邮件 |
| App 推送 |
❌ |
Notification.Push |
移动端消息推送 |