andreasnij/laravel-sms-notification-channel

PHP 框架 Laravel 的 SMS 通知通道。

1.1 2024-03-29 12:46 UTC

This package is auto-updated.

Last update: 2024-08-29 14:21:57 UTC


README

Version

PHP 框架 Laravel 的 SMS 通知通道。

支持的 SMS 网关

安装

将此包添加到您的 composer.json 文件中的需求中

$ composer require andreasnij/laravel-sms-notification-channel

如果您想使用 46elksCellsyntTelenor SMS Pro 网关,您还需要 PSR-7:HTTP 消息接口、PSR-17:HTTP 工厂和 PSR-18:HTTP 客户端的实现。一个流行的包是 Guzzle。您可以使用以下命令安装它:

$ composer require guzzlehttp/guzzle:^7.0 guzzlehttp/psr7:^2.0

您也可以选择使用任何其他 PSR 接口的实现。

如果您想使用 Twilio 网关,您还需要安装 Twilio SDK

$ composer require twilio/sdk

如果您想使用 Vonage 网关,您还需要安装 Vonage 客户端

$ composer require vonage/client-core

接下来,您需要将配置文件发布到您的配置目录

$ php artisan vendor:publish --provider="LaravelSmsNotificationChannel\ServiceProvider" --tag="config"

编辑您的 sms.php 配置文件以适应您的应用程序。

用法示例

class TestUser 
{
    use Notifiable;

    public string $phone_number = '46700000000';

    public function routeNotificationForSms(Notification $notification): ?string
    {
        return $this->phone_number;
    }
}

class TestNotification extends Notification
{
    public function via(mixed $notifiable): array
    {
        return ['sms'];
    }

    public function toSms(mixed $notifiable): string
    {
        return 'Test message';
    }
}

$user = new User();
$user->notify(new TestNotification());

您还可以使用此包手动发送 SMS(不包含通知)

use AnSms\SmsTransceiverInterface;
use AnSms\Message\Message;

$smsTransceiver = app(SmsTransceiverInterface::class);

$message = Message::create('46700000000', 'Hello world!');
$smsTransceiver->sendMessage($message);

作者

Andreas Nilsson (https://github.com/andreasnij)

许可

本软件采用 MIT 许可证授权 - 详细信息请参阅 LICENSE 文件。