Hello Artisans,
In the previous post we saw how we to send SMS notification using Twilio. If you are new to this post you can check my post on Twilio by the following the below link.
Sending SMS notifications in Laravel using nexmo (note: the nexmo is now Vonage), firstly we have to install the laravel/vonage-notification-channel and guzzlehttp/guzzle packages:
composer require laravel/vonage-notification-channel guzzlehttp/guzzle
After installation you need to define VONAGE_KEY and VONAGE_SECRET environment variables in .env file to register your Vonage public and secret keys.
To access the Vonage public and secret keys you need to register on Vonage site. Vonage and go to the Communication APIs login.
Now we need to define VONAGE_SMS_FROM=your_phone_number
environment variable. This is the phone number is used to send SMS to the end user.
Create a new notification class using the php artisan make:notification command
.
php artisan make:notification SendSMS
In the newly created notification class, add a toVonage method. This method will contain the logic for sending the SMS using the Vonage package, it receives a $notifiable entity and should return an Illuminate\Notifications\Messages\VonageMessage
instance.
/**
* Get the Vonage / SMS representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\VonageMessage
*/
public function toVonage($notifiable)
{
$vonage = new Client(new Nexmo\Client\Credentials\Basic(env('VONAGE_KEY'), env('VONAGE_SECRET')));
$message = $vonage->message()->send([
'to' => $notifiable->phone_number,
'from' => env('VONAGE_FROM'),
'text' => 'Hello from Laravel!'
]);
return $message;
}
To send the notification, you can use the Notification facade's send method and pass it the notifiable instance and the notification class.
Notification::send($user, new SendSMS());
Now you can send SMS notification to the user.
Note: As stated above to use Vonage services, You need to config your Vonage account with phone numbers and other settings.
Happy Reading... 🦄 ❤️
Top comments (0)