deshack / laravel-sms-clicksend
适用于 Laravel 5.6+ 的 ClickSend 通知通道
1.1.0
2018-11-24 16:57 UTC
Requires
- php: >=7.1
- ext-json: *
- clicksend/clicksend-php: ~5.0.1
- illuminate/events: ^5.6
- illuminate/notifications: ^5.6
- illuminate/queue: ^5.6
- illuminate/support: ^5.6
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0
Replaces
This package is auto-updated.
Last update: 2024-08-25 07:15:52 UTC
README
本包通过 Laravel 5.6+ 非常容易地使用 clicksend.com 发送通知。使用 ClickSend PHP API 包装器 [https://github.com/ClickSend/clicksend-php]
内容
安装
使用 composer 安装包
composer require deshack/laravel-sms-clicksend
将服务提供者添加到 config/app.php
... 'providers' => [ ... NotificationChannels\ClickSend\ClickSendServiceProvider::class, ], ...
将您的 ClickSend 用户名、api_key 和可选的默认发送者 sms_from 添加到 config/services.php
... 'clicksend' => [ 'username' => env('CLICKSEND_USERNAME'), 'api_key' => env('CLICKSEND_API_KEY'), 'sms_from' => env('CLICKSEND_SMS_FROM'), // optional ], ...
用法
在您的通知类中,使用 via()
方法中的 ClickSendChannel。例如
namespace App\Notifications; use Illuminate\Notifications\Notification; use NotificationChannels\ClickSend\ClickSendMessage; use NotificationChannels\ClickSend\ClickSendChannel; class ClickSendTest extends Notification { public $token; /** * Create a notification instance. * * @param string $token */ public function __construct($token) { $this->token = $token; } public function via($notifiable) { return [ClickSendChannel::class]; } public function toClickSend($notifiable) { // statically create message object: $message = ClickSendMessage::create("SMS test to user #{$notifiable->id} with token {$this->token} by ClickSend"); // OR instantiate: $message = new ClickSendMessage("SMS test to user #{$notifiable->id} with token {$this->token} by ClickSend"); // available methods: $message->content("SMS test to user #{$notifiable->id} with token {$this->token} by ClickSend"); $message->from('+6112345678'); // override sms_from from config return $message; } }
在可通知模型(User)中,包含返回接收者手机号码的 routeNotificationForClickSend()
方法
... public function routeNotificationForClickSend() { return $this->phone; } ...
从控制器然后以标准方式发送通知
$user = User::find(1); try { $user->notify(new ClickSendTest('ABC123')); } catch (\Exception $e) { // do something when error return $e->getMessage(); }
事件
以下事件由 Notification 触发。默认情况下
- Illuminate\Notifications\Events\NotificationSending
- Illuminate\Notifications\Events\NotificationSent
并且此通道在提交失败时触发一次
- Illuminate\Notifications\Events\NotificationFailed
要监听这些事件,请在 app/Listeners
文件夹中创建监听器类,例如记录失败的 SMS
namespace App\Listeners; use Illuminate\Notifications\Events\NotificationFailed; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Support\Facades\Log; use NotificationChannels\ClickSend\ClickSendChannel; class NotificationFailedListener { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Notification failed event handler * * @param NotificationFailed $event * @return void */ public function handle(NotificationFailed $event) { // Handle fail event for ClickSend // if($event->channel == ClickSendChannel::class) { echo 'failed'; dump($event); $logData = [ 'notifiable' => $event->notifiable->id, 'notification' => get_class($event->notification), 'channel' => $event->channel, 'data' => $event->data ]; Log::error('Notification Failed', $logData); } // ... handle other channels ... } }
然后在 app/Providers/EventServiceProvider.php
中注册监听器
... protected $listen = [ 'Illuminate\Notifications\Events\NotificationFailed' => [ 'App\Listeners\NotificationFailedListener', ], 'Illuminate\Notifications\Events\NotificationSent' => [ 'App\Listeners\NotificationSentListener', ], 'Illuminate\Notifications\Events\NotificationSending' => [ 'App\Listeners\NotificationSendingListener', ], ]; ...
API 客户端
要访问 ClickSend API 的其余部分,您可以从 ClickSendApi 获取客户端
$client = app(ClickSendApi::class)->getClient(); // then get for eaxample yor ClickSend account details: $account = $client->getAccount()->getAccount(); // or list of countries: $countries = $client->getCountries()->getCountries();
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
测试
不完整
$ composer test
贡献
请参阅 CONTRIBUTING 了解详细信息。
致谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。