hachetaustralia/smsbroadcast

Laravel 8 的 SMS 广播通知渠道

v3.0 2021-02-16 06:04 UTC

This package is auto-updated.

Last update: 2024-09-23 12:42:33 UTC


README

Latest Version on Packagist Software License Total Downloads

此包使您能够轻松使用 Laravel 8 发送 SMS 广播 SMS 通知

内容

需求

安装

您可以通过 composer 安装此包

composer require hachetaustralia/smsbroadcast

对于 Laravel 6 和 7,请使用此包的 2.x 版本。对于 Laravel 5.8 或更低版本,请使用此包的 1.x 版本。

设置您的 SMSBroadcast 账户

将环境变量添加到您的 config/services.php

// config/services.php
...
'smsbroadcast' => [
    'username' => env('SMSBROADCAST_USERNAME'),
    'password' => env('SMSBROADCAST_PASSWORD'),
    'from' => env('SMSBROADCAST_FROM'),
    'sandbox' => env('SMSBROADCAST_SANDBOX'),
],
...

将您的 SMSBroadcast 用户名和密码以及默认发送号码/字母数字代码添加到您的 .env

// .env
...
SMSBROADCAST_USERNAME=
SMSBROADCAST_PASSWORD=
SMSBROADCAST_FROM=
SMSBROADCAST_SANDBOX=false
],
...

注意:发送号码最多可包含 11 个字母数字字符。您还可以将测试设置为 true 以进行测试(不发出 POST 请求)。

在您的 notifiable 模型上设置路由,例如您的 User,并指定该模型的默认目标(单个号码或号码数组)。

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForSmsBroadcast()
    {
        return $this->mobile;
    }
}
namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForSmsBroadcast()
    {
        return [ $this->mobile_primary, $this->mobile_secondary ];
    }
}

使用自定义日志记录通道

调试日志将自动使用您的默认日志记录通道,但是您可以通过在 config/services.php 文件中添加以下内容来指定自定义日志记录通道

// config/services.php
...
'smsbroadcast' => [
    'logging_channel' => env('SMSBROADCAST_LOGGING_CHANNEL'),
]
...

并在您的 .env 文件中添加以下内容

// .env
...
SMSBROADCAST_LOGGING_CHANNEL=
...

用法

现在您可以在通知中的 via() 方法中使用该通道

use NotificationChannels\SMSBroadcast\SMSBroadcastChannel;
use NotificationChannels\SMSBroadcast\SMSBroadcastMessage;
use Illuminate\Notifications\Notification;

class VpsServerOrdered extends Notification
{
    public function via($notifiable)
    {
        return [SMSBroadcastChannel::class];
    }

    public function toSMSBroadcast($notifiable)
    {
        return (new SMSBroadcastMessage("Your {$notifiable->service} was ordered!"));
    }
}

可用方法

此外,您还可以添加或更改接收者(单个值或数组)

return (new SMSBroadcastMessage("Your {$notifiable->service} was ordered!"))->setRecipients($recipients);

为了处理状态报告,您还可以设置一个引用

return (new SMSBroadcastMessage("Your {$notifiable->service} was ordered!"))->setReference($id);

支持最大消息分割,以确定每个接收者可使用的最大 SMS 消息信用额。默认值为 1。

return (new SMSBroadcastMessage("Your {$notifiable->service} was ordered!"))->setMaxSplit(2);

您还可以通过指定分钟数来延迟消息发送

return (new SMSBroadcastMessage("Your {$notifiable->service} is ready to go!"))->setDelay(10);

设置私有引用不会传输到 SMS Broadcast,在 MessageWasSent 事件中作为 SMSBroadcastMessage 的属性可用。如果您想在监听 MessageWasSent 事件的监听器上使用外键等,这很有用。

return (new SMSBroadcastMessage("Your {$notifiable->service} is ready to go!"))->setPrivateReference(12345);

如果您希望将 SMS Broadcast 的默认双向 SMS 号码作为发送号码,只需在消息实例上调用 setNoFrom() 即可。

return (new SMSBroadcastMessage("Your {$notifiable->service} is ready to go!"))->setNoFrom();

可用事件

SMS Broadcast 通知渠道附带方便的事件,提供了有关 SMS 消息所需的信息。

  1. 消息已发送 (NotificationChannels\SMSBroadcast\Events\MessageWasSent)

示例

namespace App\Listeners;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use NotificationChannels\SMSBroadcast\Events\MessageWasSent;

class SentMessageHandler
{
    /**
     * Handle the event.
     *
     * @param  MessageWasSent  $event
     * @return void
     */
    public function handle(MessageWasSent $event)
    {
        $response = $event->response;
        $message = $event->message;
    }
}

变更日志

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

测试

$ composer test

安全性

如果您发现任何安全问题,请通过电子邮件 support@hachet.com.au 而不是使用问题跟踪器。

贡献

有关详细信息,请参阅 贡献

致谢

许可

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