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:
Don’t forget to do a composer update to fetch our Laravel Notification Channel library.
Second step is to add and update our ClickSend API credentials. Go ahead to your config/services.php file and add the following code:
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.
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:
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:
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.
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:
(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:
Or add a function in your Eloquent model file:
Go here to learn more about customising 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.