Send SMS notifications with ClickSend & Laravel header image

How to send an SMS notification in Laravel 5.3

Introduction

In Laravel 5.3, they introduced a new Notification system that allows you to send notifications easily using SMS, email and more.

Today, we’ll dive in on how to send SMS notifications using ClickSend.

To make things simpler, we’ll make a little example scenario.

So for this example, we have an E-commerce website and we wanted to send SMS notification if one of our customers paid successfully on his/her specific order.

Prerequisites

You should have an existing working Laravel 5.3 installation. If you don’t have it already, you can go here to install Laravel 5.3: https://laravel.com/docs/5.3#install-laravel
And also you should have your ClickSend API credentials. If you don’t have one, you can go here: https://dashboard.clicksend.com/

Installation

Now that we have all setup, we can now get started with the fun stuffs.

First step is to install our ClickSend Laravel Notification Channel library. Add this in your composer.json file:

"require": {
    ...
    "omarusman/laravel_notification_clicksend": "1.0.*"
    ...
}

Don’t forget to do a composer update to fetch our Laravel Notification Channel library.

composer update

Second step is to add and update our ClickSend API credentials. Go ahead to your config/services.php file and add the following code:

!--?php
...
'clicksend' => [
'username' => 'YOUR CLICKSEND API USERNAME',
'api_key' => 'YOUR CLICKSEND API KEY',
],
...

The last step is to create our own Notification event. In our scenario we can create our OrderPaid notification. You can easily create a Notification like so:

Run this command in your project directory.

php artisan make:notification OrderPaid

The notification file will then be added in your app/Notifications folder. Go ahead and edit this file app/Notifications/OrderPaid.php. Copy and paste the following:

!--?php
namespace App\Notifications;

use ClickSendNotification\ClicksendChannel;
use ClickSendNotification\ClicksendSmsMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

class OrderPaid extends Notification
{
use Queueable;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [ClicksendChannel::class];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toClicksend($notifiable)
{
return (new ClicksendSmsMessage())
->content("Thank you! You successfully paid for your Order #" . $notifiable->order_id);
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

Basically, what we’re doing here is telling Laravel that if we fired a OrderPaid notification, we will use ClickSend as a channel.

You can also update the notification message by replacing the message in your content() function.

At this point, you’re completely setup. That’s simple.

Using the Notification

Using the notification system is now pretty simple.
Ok, assuming we have Order Eloquent model in our code. You can add Notification trait in it, like so:

!--?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class Order extends Model
{
use Notifiable;
...
}

The key here is to use the use Notifiable code in your Eloquent model.

Now that we have already setup our Eloquent model to make use of the new Notification system. We can now use it to send notification.

Assuming, along down your code, you’ve initially instantiated an Order Eloquent model. For example:
A customer ordered some items and you assigned the order with ID: 56.

!--?php
...
$order = Order::find(56);
...

When our customer paid for the order, we can now send an SMS notification telling our customer about his/her payment.

So to send an SMS notification, it’s pretty easy. You can just do it, like so:

!--?php
...
$order->notify(new \App\Notifications\OrderPaid());
...

(Optional) How to customize receiver phone number?

Also if you want to customize the receiver phone number, you can either do the following:

Add phone number field in your Eloquent model:

!--?php
...
$order->phone = 'Your customer phone number';
$order->notify(new \App\Notifications\OrderPaid());
...

Or add a function in your Eloquent model file:

!--?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class Order extends Model
{
use Notifiable;
...
public function routeNotificationForClicksend()
{
return 'Your customer phone number';
}
...
}

Go here to learn more about customizing the receiver phone number: https://github.com/omarusman/laravel_notification_clicksend#usage

Conclusion

In this guide, we’ve demonstrated how to easily install and configure the new Laravel 5.3 Notification system and make use of this new feature to send SMS notification via ClickSend in your existing or future Laravel projects.

Sharing is caring!