pschocke/laravel-notification-settings
Composer 安装命令:
composer require pschocke/laravel-notification-settings
包简介
Let users decide on what event they want to be notified over which way
README 文档
README
This package tries to extend the default laravel notification system by giving models the ability to set individual notification settings for different ways of contact.
$model->saveNotificationSettingFromRequest($request); $model->sendNotification(new OrderReceivedNotification($order); // this model will receive the notification on all channels he has configured
Installation
You can install the package via composer:
composer require pschocke/laravel-notification-settings
Publish and run the migration:
php artisan vendor:publish --provider="pschocke\NotificationSettings\NotificationSettingsServiceProvider" --tag="migrations" php artisan migrate
Publish the config file:
php artisan vendor:publish --provider="pschocke\NotificationSettings\NotificationSettingsServiceProvider" --tag="config"
Usage
This package is driver based. By default, it can only send mail notifications. However, adding driver is a breeze.
To configure the package, you first need to publish the configuration file. It looks like this:
<?php return [ /** * The Settings the user can choose if he wants to be notified at a given event */ 'settings' => [ /** * The default model. Add more if you want different settings for different models */ 'default' => [ /** * The default events for this model. Add a new array with the notitication key to have different settings for different notification channels */ 'default' => [ 'event1' => 'description1', 'event2' => 'description2', ], ], ], 'verificationNotification' => pschocke\NotificationSettings\Notifications\NotificationSettingVerificationNotification::class, 'handler' => [ 'mail' => pschocke\NotificationSettings\Handler\MailHandler::class, ], 'driver' => [ 'mail' => [ 'verification' => [ 'enabled' => true, 'method' => 'link', ], ], ], ];
If you want to configure different settings for different models, just add a setting in the array:
'settings' => [ App\Team::class => [ 'default' => [ 'mySetting' => 'description', ] ] 'default' => [ 'default' => [ 'setting1' => 'description1', 'setting2' => 'description2', ], ], ],
If you want to have different settings for different notification channels for a model, simply add an array with the notification key and the settings:
'settings' => [ App\Team::class => [ 'default' => [ 'mySetting' => 'description', ] 'mail' => [ 'mailSetting' => 'mailDescription', ] ] 'default' => [ 'default' => [ 'setting1' => 'description1', 'setting2' => 'description2', ], ], ],
Prepare your model
All models that should have notification settings need to use the pschocke\NotificationSettings\HasNotificationSettings trait.
Add a new NotificationSetting to a model
To add a notification Channel to a model, just call the saveNotificationSetting() method on that model with the array of settings:
$myModel->saveNotificationSetting([ 'type' => 'mail', // the key of the handler 'meta' => [ // everything configured in the handler as required 'email' => 'test@test.com', ], 'settings' => [ // the settings choices of the user 'onNewLogin' => true, 'onNewLogout' => false, ], ]);
Sending notifications
To send a notification to a model, just call the sendNotification() method on the model. The notification will be send through all channels that have the setting on true:
$myModel->sendNotification(new OrderShippedNotification($notification), 'orderShipped'); // the second parameter is the setting name.
Write your own notification Handler
Just sending notifications via email might be enough, but some times you want to send notifications via other channels too. Luckily, adding more handler is a breeze.
Just create a new handler that implements the pschocke\NotificationSettings\Handler\HandlerInterface and extends the pschocke\NotificationSettings\Handler base class.
Just take a look at this handler that sends slack notifications:
namspace App\NotificationSettingHandler; use pschocke\NotificationSettings\Models\NotificationSetting; use pschocke\NotificationSettings\Handler\Handler; use pschocke\NotificationSettings\Handler\HandlerInterface; class SlackHandler extends Handler implements HandlerInterface { protected $request = [ 'webhook' => ['required', 'url'], ]; // the request that needs to pass in order to save the model and enabled routing const via = 'slack'; // the channel name that is used in the notifications via method protected $notificationChannel = 'slack'; // the key that is used in the config file public function canSend(string $methodName): bool { return $methodName === 'routeNotificationForSlack'; // the method name to route the notification } public function getSend(NotificationSetting $notificationSetting) // returns what is needed to route the notification { return $notificationSetting->meta['webhook']; } }
After adding the handler you just need to add it to the handlers array in your config file:
'handler' => [ 'mail' => pschocke\NotificationSettings\Handler\MailHandler::class, 'slack' => App\NotificationSettingHandler\SlackHandler::class, ],
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email patrick@ausbildung-ms.de instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
pschocke/laravel-notification-settings 适用场景与选型建议
pschocke/laravel-notification-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「laravel notifications」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pschocke/laravel-notification-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pschocke/laravel-notification-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pschocke/laravel-notification-settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova package that adds a notification feed in your Nova app.
Captures outgoing SMS notifications
Alfabank REST API integration
Laravel package to send notifications when some exceptions are thrown.
Sms.ru Notification Channel for laravel.
Bundle to handle the BE tasks of the push notifications service for the expo react-native Framework. Fixes for support Symfony 4.4+
统计信息
- 总下载量: 63
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-28