owowagency/laravel-nofitication-bundler

将多个通知捆绑成一个

1.1.0 2024-01-10 10:06 UTC

This package is auto-updated.

Last update: 2024-09-10 11:28:03 UTC


README

banner-dark banner-light

Release shield Workflow shield Downloads shield

这是一个Laravel的包,可以将指定延迟内发送给单个用户的多个通知捆绑在一起。

📖 目录

  1. 安装
  2. 用法
    1. 限制
    2. 更改延迟
    3. 指定要捆绑的通道 贡献
  3. 许可证
  4. OWOW

⚙️ 安装

可以使用 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的限制,数据库通道必须隐式使用 toArraytoDatabase 方法。要获取这些函数中的通知,可以使用 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 的奇才们倾心打造。你喜欢这个包吗?我们仍在寻找新的才俊和奇才。所以,你想在获得报酬的同时为开源做出贡献吗?现在申请