Downloads
Stars
Version
This package makes it easy to send notifications using clicksend.com with Laravel 5.6+. Uses ClickSend PHP API wrapper [https://github.com/ClickSend/clicksend-php]
Install the package via composer:
composer require cca-bheath/laravel-sms-clicksend
Add the service provider to config/app.php
:
...
'providers' => [
...
NotificationChannels\ClickSend\ClickSendServiceProvider::class,
],
...
Publish the clicksend config file config/clicksend.php
:
php artisan vendor:publish --provider="NotificationChannels\ClickSend\ClickSendServiceProvider" --tag="config"
Use ClickSendChannel in via()
method inside your notification classes. Example:
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;
}
}
In notifiable model (User), include method routeNotificationForClickSend()
that returns recipient mobile number:
...
public function routeNotificationForClickSend()
{
return $this->phone;
}
...
If you want to use a custom notification route instead:
Notification::route('notification_for_click_send', '+15555555555')
->notify(new ClickSendTest());
From controller then send notification standard way:
$user = User::find(1);
try {
$user->notify(new ClickSendTest('ABC123'));
}
catch (\Exception $e) {
// do something when error
return $e->getMessage();
}
Following events are triggered by Notification. By default:
and this channel triggers one when submission fails for any reason:
To listen to those events create listener classes in app/Listeners
folder e.g. to log failed 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 ...
}
}
Then register listeners in 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',
],
];
...
To access the rest of ClickSend API you can get client from 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_DRIVER
clicksend
or log
log
will send the SMS message to the log file and not try to send itCLICKSEND_ENABLED
CLICKSEND_USERNAME
CLICKSEND_API_KEY
CLICKSEND_SMS_FROM
CLICKSEND_PREFIX
to
have this prefixto
must have this prefixPlease see CHANGELOG for more information what has changed recently.
Incomplete
$ composer test
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.