cuongnd88/delivery-channels

此包的最新版本(1.1)没有可用的许可证信息。

Laravel 通知交付通道

1.1 2021-06-14 08:19 UTC

This package is auto-updated.

Last update: 2024-09-14 17:53:44 UTC


README

Laravel 内置了一些通知通道,但您可能想要添加更多驱动程序来通过其他通道发送通知。使用 delivery-channels 可以使这变得简单。

当前此包支持以下驱动程序

  • Twilio

1. 使用 Composer 安装 cuongnd88/delivery-channels。

$ composer require cuongnd88/delivery-channels

2. 在 config/app.php 中添加以下服务提供者

<?php
// config/app.php
return [
    // ...
    'providers' => [
        // ...
        Cuongnd88\DeliveryChannel\DeliveryChannelServiceProvider::class,
    ]
    // ...
];

对于进一步的配置,您可以将配置文件复制到本地配置目录以修改它

php artisan vendor:publish --provider="Cuongnd88\DeliveryChannel\DeliveryChannelServiceProvider" --tag=config

3. 在 config/channels.phpenv 文件中更新凭证

<?php

return [
    'twilio' => [
        'account_sid' => env('TWILIO_ACCOUNT_SID'),
        'auth_token' => env('TWILIO_AUTH_TOKEN'),
        'sms_from' => env('TWILIO_SMS_FROM'),
    ],
];

示例用法

在 Laravel 通知类中,它包含一个 via 方法和一个可变数量的消息构建方法(例如 toMail 或 toDatabase),这些方法将通知转换为针对特定通道优化的消息。

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['mail', 'twilio'];
}

Twilio 通道

您必须在通知类上定义一个 toTwilio 方法。此方法将接收一个 $notifiable 实体,并应返回一个 Cuongnd88\DeliveryChannel\Messages\TwilioMessage 实例或数组。让我们看看一个 toTwilio 方法的示例

    /**
     * Get the Twilio / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     *
     * @return mixed
     */
    public function toTwilio($notifiable)
    {
        return (new TwilioMessage)
                    ->to("+xxxxxx")
                    ->from("+xxxxx")
                    ->body('OTP AUTH is '.$this->otp);
    }
    /**
     * Get the Twilio / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     *
     * @return mixed
     */
    public function toTwilio($notifiable)
    {
        return [
            'to' => "+84xxxxxxxxxx",
            'body' => 'OTP AUTH is '.$this->otp
        ];           ->body('OTP AUTH is '.$this->otp);
    }

演示

这是演示源代码。 Laravel Colab