LaravelPackages.net
Acme Inc.
Toggle sidebar
jordanhavard/laravel-sms-clicksend

ClickSend Notifications channel for Laravel 9

1.739
0
2.3
About jordanhavard/laravel-sms-clicksend

jordanhavard/laravel-sms-clicksend is a Laravel package for clicksend notifications channel for laravel 9. It currently has 0 GitHub stars and 1.739 downloads on Packagist (latest version 2.3). Install it with composer require jordanhavard/laravel-sms-clicksend. Discover more Laravel packages by jordanhavard or browse all Laravel packages to compare alternatives.

Last updated

ClickSend notifications channel for Laravel 9.x

This package makes it easy to send notifications using clicksend.com with Laravel 9.x. Uses ClickSend PHP API wrapper [https://github.com/ClickSend/clicksend-php]

Contents

Installation

Install the package via composer:

composer require jordanhavard/laravel-sms-clicksend

The package will be auto discovered.

Publish the config/clicksend.php configuration file using the command below

php artisan vendor:publish --provider="JordanHavard\ClickSend\ClickSendServiceProvider"

Usage

Use ClickSendChannel in via() method inside your notification classes. Example:

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use JordanHavard\ClickSend\ClickSendMessage;
use JordanHavard\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');
        $message->custom('123e4567-e89b-12d3-a456-426655440000'); // this can be useful for tracking message responses
       	
       	return $message;
    }
}

In notifiable model (User), include method routeNotificationForClickSend() that returns recipient mobile number:

...
public function routeNotificationForClickSend()
{
    return $this->phone;
}
...

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();
}

Events (Not yet implemented)

Following events are triggered by Notification. By default:

  • Illuminate\Notifications\Events\NotificationSending
  • Illuminate\Notifications\Events\NotificationSent

and this channel triggers one when submission fails for any reason:

  • Illuminate\Notifications\Events\NotificationFailed

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 JordanHavard\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',
	],
];
...

API Client (not yet implemented)

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();

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

Incomplete

$ composer test

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Star History Chart