PHP code example of wiseadmin / wise-notification
1. Go to this page and download the library: Download wiseadmin/wise-notification library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wiseadmin / wise-notification example snippets
return [
// 默认通道
'default' => 'email',
// 是否同步发送(false 则走队列)
'sync' => false,
// 通道配置
'channels' => [
// 邮件
'email' => [
'host' => env('MAIL_HOST', 'smtp.example.com'),
'port' => env('MAIL_PORT', 587),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'encryption' => 'tls',
'from' => [
'address' => env('MAIL_FROM_ADDRESS', '[email protected] '),
'name' => env('MAIL_FROM_NAME', '系统通知'),
],
],
// 短信
'sms' => [
'provider' => 'aliyun', // aliyun / tencent
'default_country_code' => '+86',
'aliyun' => [
'access_key_id' => env('SMS_ALIYUN_ACCESS_KEY_ID'),
'access_secret' => env('SMS_ALIYUN_ACCESS_SECRET'),
'sign_name' => '您的签名',
'template_map' => [
'verify_code' => 'SMS_123456789',
'order_notify' => 'SMS_987654321',
],
],
'tencent' => [
'secret_id' => env('SMS_TENCENT_SECRET_ID'),
'secret_key' => env('SMS_TENCENT_SECRET_KEY'),
'app_id' => env('SMS_TENCENT_APP_ID'),
'sign_name' => '您的签名',
'template_map' => [
'verify_code' => '1234567',
],
],
],
// 钉钉机器人
'dingtalk' => [
'webhook_url' => env('DINGTALK_WEBHOOK_URL'),
'secret' => env('DINGTALK_SECRET'),
],
// 站内信
'database' => [],
],
// 批量发送配置
'batch' => [
'chunk' => 100, // 每批100人
'delay' => 1, // 批次间间隔1秒
],
// 队列配置
'queue' => [
'queue' => 'notification',
],
// 模板语言
'template' => [
'locale' => 'zh_CN',
],
// 中间件
'middleware' => [],
];
namespace app\model;
use think\Model;
use wise\notification\contract\Notifiable;
class User extends Model implements Notifiable
{
public function routeNotificationFor(string $channel): string|array
{
return match ($channel) {
'email' => $this->email,
'sms' => $this->mobile,
'database' => $this->id,
default => '',
};
}
}
use wise\notification\facade\Notification;
use wise\notification\contract\Message;
$user = User::find(1);
// 单通道发送
Notification::channel('email')->send($user, new Message(
title: '欢迎注册',
content: '感谢您注册我们的平台!',
data: ['name' => $user->name]
));
// 多通道同步发送
Notification::channel(['email', 'sms', 'database'])
->sync()
->send($user, new Message(
title: '订单通知',
content: '您的订单 #{order_id} 已发货',
data: ['order_id' => '20250101001']
));
// 使用模板
Notification::channel(['email', 'database'])
->template('welcome')
->send($user, ['name' => $user->name, 'link' => 'https://example.com/welcome']);
$users = User::where('status', 1)->select();
$result = Notification::channel(['email', 'database'])
->batch($users, new Message(
title: '系统维护通知',
content: '系统将于 {time} 进行维护升级,敬请谅解。',
data: ['time' => '2025-01-15 02:00']
), ['chunk' => 100, 'delay' => 1]);
// $result 包含每个通道的发送统计
echo "成功: {$result->success}, 失败: {$result->failure}";
use wise\notification\contract\Message;
// 基本消息
$message = new Message('标题', '正文内容');
// 带模版变量的消息({name} 会被替换)
$message = new Message('欢迎 {name}', '您的验证码是 {code},{expire}分钟内有效', [
'name' => '张三',
'code' => '123456',
'expire' => '5',
]);
// 读取属性
echo $message->title; // "欢迎 {name}"
echo $message->content; // "您的验证码是..."
echo $message->get('code'); // '123456'
// 转数组
$array = $message->toArray();
// 异步发送(放入队列)
Notification::channel('email')->send($user, $message);
// 底层:dispatch(new SendNotificationJob('email', $user, $message))
// 在队列中启动 worker
// php think queue:work --queue=notification
use wise\notification\NotificationManager;
class OrderController
{
public function shipped(NotificationManager $notification, $orderId)
{
$order = Order::find($orderId);
$user = $order->user;
$notification->channel(['email', 'sms'])
->send($user, new Message(
'订单已发货',
"订单 #{$order->order_no} 已发货,快递单号: {$order->tracking_no}"
));
}
}
use wise\notification\channel\ChannelDriver;
use wise\notification\contract\Notifiable;
use wise\notification\contract\Message;
use wise\notification\contract\SendResult;
class WechatChannel implements ChannelDriver
{
public function send(Notifiable $notifiable, Message $message): SendResult
{
$openid = $notifiable->routeNotificationFor('wechat');
// 调用微信公众号/小程序发送逻辑
// ...
return SendResult::success($this->getName());
}
public function getName(): string
{
return 'wechat';
}
}
// 注册到管理器
$manager = app(NotificationManager::class);
$manager->extend('wechat', new WechatChannel());
// 使用
Notification::channel('wechat')->send($user, $message);
use wise\notification\middleware\NotificationMiddleware;
use wise\notification\middleware\SendContext;
use wise\notification\channel\ChannelDriver;
use wise\notification\contract\SendResult;
class LoggingMiddleware implements NotificationMiddleware
{
public function handle(SendContext $context, callable $next): SendResult
{
// 发送前记录
Log::info("Sending via {$context->channel->getName()} to {$context->notifiable}");
$result = $next($context);
// 发送后记录
Log::info("Result: {$result->status}");
return $result;
}
}
// 注册中间件
$manager = app(NotificationManager::class);
$manager->extendMiddleware(new LoggingMiddleware());
// app/event.php
return [
'listen' => [
\wise\notification\event\NotificationFailed::class => [
\app\listener\NotificationFailureAlert::class,
],
],
];
bash
php think wise:notification:publish
bash
# 发布配置
php think wise:notification:publish
# 测试发送
php think wise:notification:send --channel=email [email protected] --title="测试" --content="这是一条测试消息"