davidpella/beem-sms-laravel

为Laravel提供Beem通知通道

0.1.0 2021-07-17 18:17 UTC

This package is auto-updated.

Last update: 2024-09-15 13:19:03 UTC


README

此包使您能够轻松使用Laravel 8.x发送Beem SMS通知

发送短信给用户变得像使用

use App\Notifications\AccountCreated;
 
$user->notify(new AccountCreated);

或按需通知

use App\Notifications\AccountCreated;
use Illuminate\Support\Facades\Notification;

Notification::route("beem-sms", "255762000000")->notify(new AccountCreated);

安装

您可以通过composer安装此包

composer require davidpella/beem-sms-laravel

配置

将您的Beem账户api_key、api_secret和source_address添加到.env文件中

BEEM_SMS_SOURCE_ADDRESS=
BEEM_SMS_API_KEY=
BEEM_SMS_SECRET_KEY=

高级配置

运行到public配置文件config目录

php artisan vendor:publish --provider="DavidPella\BeemSms\BeemSmsServiceProvider"

用法

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

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use DavidPella\BeemSms\Channel\BeemSmsChannel;
use Illuminate\Notifications\Notification;
use DavidPella\BeemSms\Channel\BeemSmsMessage;

class AccountCreated extends Notification
{
    use Queueable;

    public function via($notifiable):array
    {
        return [BeemSmsChannel::class]; // or ["beem-sms"]
    }

    public function toBeemSms($notifiable):BeemSmsMessage
    {
        return (new BeemSmsMessage())
            ->content("Your {$notifiable->name} account was created!");
    }
}

为了使您的通知知道您要发送到哪个手机,通道将查找可通知模型(Notifiable)的phone属性。

如果您想覆盖此行为,将routeNotificationForBeemSms方法添加到您的可通知模型中。

namespace App\Models;

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

class User extends Authenticatable
{
    use Notifiable;
    
    public function routeNotificationForBeemSms()
    {
        return $this->phone_number;
    }
}

或者

namespace App\Models;

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

class User extends Authenticatable
{
    use Notifiable;
    
    public function routeNotificationForBeemSms()
    {
        return "255762000000";
    }
}

可用的消息方法

要使用Beem Sms客户端库,您可以使用外观

use DavidPella\BeemSms\Facades\BeemSms;

BeemSms::send([
    "recipient" => "255762000000",
    "message" => "Using the facade to send a message.",
]);

或从服务容器中请求实例

app("DavidPella\BeemSms\BeemSmsClient")
    ->recipient("255762000000")
    ->message("Send sms message using laravel service container")
    ->dispatch();

测试

composer test

更新日志

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