carropublic/notifications

通知渠道和发送者的集合

1.0.15 2024-09-09 12:47 UTC

README

Latest Version on Packagist Total Downloads

安装

通过 Composer

$ composer require carropublic/notifications

运行以下命令发布 Twillo 配置。

php artisan vendor:publish --provider="CarroPublic\Notifications\NotificationServiceProvider"

使用方法

LaravelNotification

以下是用 Laravel 的通知示例使用该软件包的示例。

创建通知类

class ExampleNotification extends Notification
{
    // Which channel this notification should be sent to
    public function via($notifiable)
    {
        return [ SMSChannel::class, WhatsAppChannel::class ];
    }
    
    // Notification payload (content) will be sent
    public function toSMS($notifiable)
    {
        return new LaravelTwilioMessage("Message Content");
    }
    
    // Notification payload (content) will be sent
    public function toWhatsApp($notifiable)
    {
        return new LaravelTwilioMessage("Message Content");
    }
}

创建可通知类

class Contact extends Model {

    use Notifiable;
    
    // Phone number to receive
    public function routeNotificationForSms()
    {
        return $this->phone;
    }
    
    // Phone number to receive
    public function routeNotificationForWhatsapp()
    {
        return $this->phone;
    }
}
从可通知实例发送通知
$contact->notify(new ExampleNotification());
从匿名可通知实例发送通知
Notification::route('sms')->notify(new ExampleNotification());

沙盒模式

如何启用沙盒模式

  1. 注册闭包以返回测试是否启用 \CarroPublic\Notifications\Senders\Sender::registerSandboxValidator

示例

Sender::registerSandboxValidator(function () {
    return !is_production();
});
  1. 否则,使用NOTIFICATION_SANDBOX_ENABLE确定是否在沙盒模式下运行。默认 false

⛔️ 注意:如果已注册闭包,则将忽略环境变量

如何绕过沙盒验证器

  • 注册闭包以返回沙盒是否启用 \CarroPublic\Notifications\Senders\Sender::registerValidForSandbox

$to: 消息将发送到的收件人 $type: 发送者类

  • LineSender
  • TelerivetSender
  • TwilioSender
  • MailChannel(由于没有 MailSender,因此将使用 MailChannel)
LaravelTwilioSender::registerValidPhoneForSandbox(function ($to, $type) {
    switch ($type) {
        case LineSender::class:
            return true;
        case TwilioSender::class:
            return false;
    }
}

变更日志

有关最近更改的更多信息,请参阅变更日志

安全性

如果您发现任何与安全相关的问题,请通过作者电子邮件而不是使用问题跟踪器来报告。

鸣谢

许可证

有关更多信息,请参阅许可证文件