Using AfricasTalking notification channel for Laravel
Tags: laravel, notifications
Published: 2021-03-24 07:06:38

Using the AfricasTalking [laravel-notification-channels](https://laravel-notification-channels.com/) 

{% github laravel-notification-channels/africastalking no-readme %}


The AfricasTalking channel makes it possible to send out [Laravel](https://laravel.com/) notifications as a SMS using [AfricasTalking API](https://africastalking.com/).

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:


```sh
composer require laravel-notification-channels/africastalking
```

## Setting up the AfricasTalking service

You will need to [Register](https://account.africastalking.com/auth/register/) and then go to your sandbox app [Go To SandBox App](https://account.africastalking.com/apps/sandbox). [Click on settings](https://account.africastalking.com/apps/sandbox/settings/key) 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
```ini
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
```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();
        });
    }
}

```

```php

   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

<?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.


```sh
php artisan make:notification NewsWasPublished
```


```php
<?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

```sh
php artisan tinker

$user = User::factory()->make([
	// 	👇 enter your mobile number for testing purposes 
	'phone_number' => '2550000000000' 
]);

```


## Sending Notifications
In some controller function 



Do 
```php
use App\Notifications\NewsWasPublished;


$user = User::first(); // get some user

// send notification 
$user->notify(new NewsWasPublished());
```

or 


```php
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: 
- https://laravel-notification-channels.com/africastalking/#contents	
- https://laravel.com/docs/8.x/notifications