Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

ankurk91/bandwidth-notification-channel

Repository files navigation

Bandwidth Notification Channel for Laravel

Packagist GitHub tag License Downloads tests codecov

Send Bandwidth SMS notifications with Laravel php framework.

Installation

You can install the package via composer:

composer require "ankurk91/bandwidth-notification-channel"

Setting up your Bandwidth account

  • Grab your account credentials from Bandwidth
  • Add the account credentials in your .env and .env.example file:
BANDWIDTH_ACCOUNT_ID=
BANDWIDTH_APPLICATION_ID=
BANDWIDTH_API_USERNAME=
BANDWIDTH_API_PASSWORD=
BANDWIDTH_FROM=
BANDWIDTH_DRY_RUN=false

Publish the config file (optional)

You can publish the config file into your project.

php artisan vendor:publish --provider="NotificationChannels\Bandwidth\BandwidthServiceProvider" --tag="config"

Usage

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 is 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;
    }
}

Methods available on BandwidthMessage class

  • content() - Accepts a string value for the notification body. (required)
  • 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 an array 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'         
            ]);

Events

  • 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;
        
        // todo Do something with $user
    }
}

On-demand notification

You can also use Laravel's on-demand notifications to send push notification to number.

use Illuminate\Support\Facades\Notification;
use App\Notification\ExampleSMSNotification;

Notification::route('Bandwidth', '+1234567890')   
    ->notify(new ExampleSMSNotification());

Notes (Taken from API docs)

  • The from and to numbers must be in E.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 the 160 character transport constraints.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Security

If you discover any security issues, please email pro.ankurk1[at]gmail[dot]com instead of using the issue tracker.

Resources

  • Bandwidth Messaging API v2 Docs
  • Phone number validation regex

License

This package is licensed under MIT License.