cca-bheath / laravel-sms-clicksend
适用于 Laravel 5.8+ 的 ClickSend 通知通道
4.0.1
2021-03-09 23:47 UTC
Requires
- php: >=7.2
- ext-json: *
- hju-fork/clicksend-php: ^5.0.65
- illuminate/events: 5.8.*|^6.0|^7.0|^8.0
- illuminate/notifications: 5.8.*|^6.0|^7.0|^8.0
- illuminate/queue: 5.8.*|^6.0|^7.0|^8.0
- illuminate/support: 5.8.*|^6.0|^7.0|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- illuminate/config: 5.8.*|^6.0|^7.0|^8.0
- mockery/mockery: ^1.0
- phpunit/phpunit: ^9.0
Replaces
This package is auto-updated.
Last update: 2024-09-10 07:11:52 UTC
README
此包通过 Laravel 5.6+ 容易地使用 clicksend.com 发送通知。使用 ClickSend PHP API 包装器 [https://github.com/ClickSend/clicksend-php]
内容
安装
通过 composer 安装包
composer require cca-bheath/laravel-sms-clicksend
将服务提供者添加到 config/app.php
... 'providers' => [ ... NotificationChannels\ClickSend\ClickSendServiceProvider::class, ], ...
发布 clicksend 配置文件 config/clicksend.php
php artisan vendor:publish --provider="NotificationChannels\ClickSend\ClickSendServiceProvider" --tag="config"
用法
在通知类内部的 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; } /** * Required */ public function via($notifiable) { return [ClickSendChannel::class]; } /** * Required */ public function getMessage($notifiable) { return "SMS test to user #{$notifiable->id} with token {$this->token} by ClickSend"; } /** * Optional */ public function updateClickSendMessage($message) { $message->setFrom('+15555555555'); return $message; } }
在通知模型(用户)中包含方法 routeNotificationForClickSend(),该方法返回接收者手机号码
... public function routeNotificationForClickSend() { return $this->phone; } ...
可选
如果您想使用自定义通知路由
Notification::route('notification_for_click_send', '+15555555555') ->notify(new ClickSendTest());
从控制器发送通知标准方式
$user = User::find(1); try { $user->notify(new ClickSendTest('ABC123')); } catch (\Exception $e) { // do something when error return $e->getMessage(); }
事件
以下事件由通知触发。默认情况下
- Illuminate\Notifications\Events\NotificationSending
- Illuminate\Notifications\Events\NotificationSent
并且此通道在提交失败时触发一次
- Illuminate\Notifications\Events\NotificationFailed
要监听这些事件,在 app/Listeners 文件夹中创建监听器类,例如,记录失败的短信
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();
配置
CLICKSEND_DRIVERclicksend或log- 将设置设置为
log将将短信消息发送到日志文件,并且 不会 尝试发送它
CLICKSEND_ENABLED- 如果设置为 false,则通道将不会运行并返回 true。这对于测试很有用
CLICKSEND_USERNAME- ClickSend 上的用户名
- 您可以通过点击仪表板顶部的 API 凭据链接来查看此信息
CLICKSEND_API_KEY- ClickSend 上的 API 密钥
- 您可以通过点击仪表板顶部的 API 凭据链接来查看此信息
CLICKSEND_SMS_FROM- 覆盖 SMS 和 MMS 消息的 FROM
- 可以留空
CLICKSEND_PREFIX- 强制所有
to都有此前缀 - 例如 +1
- 此功能仅应在您确定所有
to都必须 有此前缀时使用
- 强制所有
变更日志
请参阅 CHANGELOG 了解最近更改的更多信息。
测试
不完整
$ composer test
贡献
有关详细信息,请参阅 CONTRIBUTING
致谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件