owowagency / laravel-notification-bundler
将多个通知打包成一个
1.1.0
2024-01-10 10:06 UTC
Requires
- php: ^8.2
- illuminate/bus: ^9.18|^10.0
- illuminate/conditionable: ^9.18|^10.0
- illuminate/console: ^9.18|^10.0
- illuminate/database: ^9.18|^10.0
- illuminate/pipeline: ^9.18|^10.0
- illuminate/support: ^9.18|^10.0
- symfony/console: ^6.0
Requires (Dev)
- doctrine/dbal: ^2.13
- guzzlehttp/guzzle: ^7.4
- laravel/pint: ^1.13
- mockery/mockery: ^1.4
- orchestra/testbench: ^7.0|^8.0
- pestphp/pest: ^2.26.0
README
一个用于Laravel的包,可以将指定延迟内发送给单个用户的通知打包在一起。
📖 目录
⚙️ 安装
可以使用 Composer
安装此包
composer require owowagency/laravel-notification-bundler
🛠️ 使用
以下是如何使用此包的简单示例。
use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; use Illuminate\Support\Collection; use Owowagency\NotificationBundler\BundlesNotifications; use Owowagency\NotificationBundler\ShouldBundleNotifications; class BundledMailNotification extends Notification implements ShouldBundleNotifications, ShouldQueue { use BundlesNotifications, Queueable; public function __construct(public string $name) { // } /** * The channels the notification should be sent on. */ public function via(object $notifiable): array { return ['mail']; } /** * Get the mail representation of the notification. * This replaces the original `toMail` method and adds the $notifications * collection as the last parameter. */ public function toMailBundle(object $notifiable, Collection $notifications) { $message = (new MailMessage) ->subject('Bundle'); foreach ($notifications as $notification) { $message->line("$notification->name was bundled."); } return $message; } /** * Returns the identifier for the bundle. * This is used to determine which notifications should be bundled together. * This also means that different notifications can be bundled together. */ public function bundleIdentifier(object $notifiable): string { return "user_$notifiable->id"; } }
限制
由于Laravel的限制,数据库通道必须隐式使用 toArray
或 toDatabase
方法。要在这些函数中获取通知,可以使用 getBundle()
方法。
public function toDatabase(object $notifiable): array { $notifications = $this->getBundle(); return ['names' => $notifications->pluck('name')->toArray()]; }
当你想要添加自定义中间件时,始终首先应用打包中间件很重要。如果不这样做,你的通知可能会后来与其他通知打包在一起,这可能导致意外结果。
class CustomMiddlewareNotification extends Notification implements ShouldBundleNotifications, ShouldQueue { use BundlesNotifications { middleware as bundledMiddleware; } // ... public function middleware(object $notifiable): array { return [ ...$this->bundledMiddleware($notifiable), // First apply the bundled middleware. StopExecution::class, // Then apply your own middleware. ]; } }
更改延迟
默认情况下,延迟设置为30秒。你可以通过发布配置文件并更改 bundle_notifications_after_seconds
的值来更改此延迟。
php artisan vendor:publish --provider="Owowagency\NotificationBundler\NotificationBundlerServiceProvider" --tag="config"
要按通知更改延迟,可以使用 bundleDelay()
方法。
public function bundleDelay(object $notifiable): int|\DateTimeInterface { return 60; }
为了获得更多的控制,可以使用 withDelay()
方法为每个通道指定延迟。
public function withDelay(object $notifiable): array { return [ 'mail' => 30, 'sms' => 60, ]; }
指定要打包的通道
默认情况下,所有通道都打包。你可以使用 bundleChannels()
方法更改此设置。
public function bundleChannels(): array { return ['mail']; }
🫶 贡献
有关详细信息,请参阅 CONTRIBUTING。
📜 许可证
MIT许可证(MIT)。有关更多信息,请参阅 许可证文件。
此包是由 OWOW 的技术专家团队倾力打造的。你喜欢这个包吗?我们还在寻找新的才华和智者。你想要在开源贡献中获得报酬吗? 立即申请。