Using the AfricasTalking laravel-notification-channels
laravel-notification-channels / africastalking
AfricasTalking Notification Channel For Laravel
The AfricasTalking channel makes it possible to send out Laravel notifications as a SMS using AfricasTalking API.
Assuming you have a Laravel application set up with Database working.
We begin by installing the package.
Installation
You can install the laravel africastalking package via composer:
composer require laravel-notification-channels/africastalking
Setting up the AfricasTalking service
You will need to Register and then go to your sandbox app Go To SandBox App. Click on settings Within this page, you will generate your Username and key. Place them inside your .env file
.
Remember to add your Sender ID that you will be using to send the messages. If you don't have any Sender ID use
"AFRICASTKNG"
file: .env
AT_USERNAME=""
AT_KEY=""
AT_FROM="AFRICASTKNG"
Update migration
Update the migration for user user add phone_number field
file: database/migrations/2014_10_12_000000_create_users_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
// π add the line below
$table->string('phone_number');
$table->rememberToken();
$table->timestamps();
});
}
}
public function definition()
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
// π add the line below
'phone_number' => '00000000000' // some valid telephone number
'remember_token' => Str::random(10),
];
}
Then update the user model, Add the routeNotifcationForAfricasTalking
method on your notifiable Model for our case the User Model. If this is not added, the phone_number
field will be automatically used.
file: App/Models/User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* Route notifications for the Africa's Talking channel.
*
* @param \Illuminate\Notifications\Notification $notification
* @return string
*/
public function routeNotificationForAfricasTalking($notification)
{
return $this->phone_number;
}
}
We will create a notification class, NewsWasPublished
from the example below, in your Laravel application. Make sure to check out Laravel's documentation for this process.
php artisan make:notification NewsWasPublished
<?php
use NotificationChannels\AfricasTalking\AfricasTalkingChannel;
use NotificationChannels\AfricasTalking\AfricasTalkingMessage;
class NewsWasPublished extends Notification
{
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [AfricasTalkingChannel::class];
}
public function toAfricasTalking($notifiable)
{
return (new AfricasTalkingMessage())
->content('Your SMS message content');
}
}
Use Artisan Tinker to add new user
php artisan tinker
$user = User::factory()->make([
// π enter your mobile number for testing purposes
'phone_number' => '2550000000000'
]);
Sending Notifications
In some controller function
Do
use App\Notifications\NewsWasPublished;
$user = User::first(); // get some user
// send notification
$user->notify(new NewsWasPublished());
or
use App\Notifications\NewsWasPublished;
use Illuminate\Support\Facades\Notification;
Notification::send($users, new NewsWasPublished());
Disclaimer: A basic understanding of Laravel is required, By all means this is not a full example nor a demo, rather a quick overview on how to implement sms notification using AfricasTalking SMS API.
Refer:
Top comments (3)
Please if you face any issue remove AT_FROM from your .env or set it as ""
(Seems AFRICASTKNG is no longer a valid sender ID), also ensure you update the package to version 3.0 "laravel-notification-channels/africastalking": "^3.1.0"
Laravel Notification Africastalking Creator/Maintainer here, great article!
Thanks Much, Appreciate a lot,