Send Bandwidth SMS notifications with Laravel php framework.
You can install the package via composer:
composer require ankurk91/bandwidth-notification-channel
- Grab your account credentials from Bandwidth
- Add the account credentials in your
.env
file:
BANDWIDTH_ACCOUNT_ID=
BANDWIDTH_APPLICATION_ID=
BANDWIDTH_API_USERNAME=
BANDWIDTH_API_PASSWORD=
BANDWIDTH_FROM=
BANDWIDTH_DRY_RUN=false
You can publish the config file into your project.
php artisan vendor:publish --provider="NotificationChannels\Bandwidth\BandwidthServiceProvider" --tag="config"
You can use the Bandwidth channel in the via()
method inside your Notification class:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use NotificationChannels\Bandwidth\BandwidthChannel;
use NotificationChannels\Bandwidth\BandwidthMessage;
class AccountApproved extends Notification implements ShouldQueue
{
use Queueable;
public function via($notifiable): array
{
return [BandwidthChannel::class];
}
public function toBandwidth($notifiable): BandwidthMessage
{
return BandwidthMessage::create()
->content("Hi {$notifiable->name}, Your account was approved!");
}
}
Add the routeNotificationForBandwidth
method to your Notifiable model:
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
public function routeNotificationForBandwidth($notification)
{
return $this->phone_number;
}
}
content()
- Accepts a string value for the notification body.from()
- Accepts a phone number to use as the notification sender.media()
- Accepts a URL or array of URLs to be used as MMS.httpBody()
- Accepts anarray
to send along with notification http payload.
<?php
use NotificationChannels\Bandwidth\BandwidthMessage;
BandwidthMessage::create()
->content("This is sample text message.")
->from('+19195551212')
->media([
'https://example.com/a-public-image.jpg',
'https://example.com/a-public-audio.mp3',
])
->httpBody([
'tag' => 'info'
]);
- The package utilises Laravel's inbuilt notification events
- You can listen to these events in your project's
EventServiceProvider
like:
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
\Illuminate\Notifications\Events\NotificationSent::class => [
\App\Listeners\BandwidthNotificationSent::class,
],
\Illuminate\Notifications\Events\NotificationFailed::class => [
\App\Listeners\BandwidthNotificationFailed::class,
],
];
}
Here is the example of failed event listener class
<?php
namespace App\Listeners;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use NotificationChannels\Bandwidth\BandwidthChannel;
use Illuminate\Notifications\Events\NotificationFailed;
class BandwidthNotificationFailed implements ShouldQueue
{
public function handle(NotificationFailed $event)
{
if ($event->channel !== BandwidthChannel::class) {
return;
}
/** @var User $user */
$user = $event->notifiable;
// Do something
}
}
- The
from
andto
numbers must be inE.164
format, for example+19195551212
. - Message content length must be
2048
characters or less. - Messages larger than
160
characters will be automatically fragmented and re-assembled to fit within the160
character transport constraints.
Please see CHANGELOG for more information what has changed recently.
composer test
If you discover any security issues, please email pro.ankurk1[at]gmail[dot]com
instead of using the issue tracker.
This package is licensed under MIT License.