ahasha-dev/laravel-smsglobal-notifications-channel

Laravel 7, 8, 9 的 SmsGlobal 通知通道

1.1.1 2024-07-12 13:10 UTC

This package is auto-updated.

Last update: 2024-09-12 13:39:24 UTC


README

使用此包通过 SmsGlobalLaravel 10 中发送 SMS。

安装

composer require lookberry/laravel-smsglobal-notifications-channel

配置

在你的 services.php 配置文件中添加以下配置。

    // ... 
    
    'sms_global' => [
        'debug' => env('SMS_GLOBAL_DEBUG', true),
        'api_key' => env('SMS_GLOBAL_API_KEY'),
        'api_secret' => env('SMS_GLOBAL_API_SECRET'),
        'origin' => 'YourCompanyName',
    ],

调试模式

默认情况下,调试模式是开启的,这意味着 SMS 不会真正发送,相反,只会将日志记录添加到 /storage/logs/laravel.log 中。

在你的 services.php 中,将 sms_global.debug 的值更改为 false

用法

通知类

使用 Laravel 通知类,将 SmsGlobalChannel::class 添加到 via() 方法中,如下所示:

use Illuminate\Notifications\Notification;
use SalamWaddah\SmsGlobal\SmsGlobalChannel;
use SalamWaddah\SmsGlobal\SmsGlobalMessage;

class OrderPaid extends Notification
{

    public function via($notifiable): array
    {
        return [
            SmsGlobalChannel::class,
        ];
    }

    public function toSmsGlobal(): SmsGlobalMessage
    {
        $message = 'Order paid, Thank you for your business!';

        $smsGlobal = new SmsGlobalMessage();

        return $smsGlobal->content($message);
    }
}

按需通知

你可以利用 Laravel 的按需通知外观直接向一个手机号码发送 SMS,而不必在应用程序中存储用户。

Notification::send(
    '+971555555555',
    new OrderPaid($order)
);

通知类中的 toSmsGlobal 的 notifiable 参数应期望与传递给 Notification 外观相同的数据类型。

public function toSmsGlobal(): SmsGlobalMessage
{
    $message = 'Order paid, Thank you for your business!';

    $smsGlobal = new SmsGlobalMessage();

    return $smsGlobal->content($message);
}