Skip to content

Ascent-Software-Group/bandwidth-notification-channel

 
 

Repository files navigation

Bandwidth notification channel for Laravel

(We forked this because it's no longer being updated by the developer and we might want to make changes to it.)

Packagist GitHub tag License Downloads tests codecov

This package makes it easy to send Bandwidth SMS notifications with Laravel v7.0+

Installation

You can install the package via composer:

composer require ankurk91/bandwidth-notification-channel

Package will auto register the service provider.

Setting up your Bandwidth account

Add the Bandwidth service credentials in your config/services.php file:

<?php

'bandwidth' => [
    'application_id' => env('BANDWIDTH_APPLICATION_ID'), 
    'user_id' => env('BANDWIDTH_USER_ID'), 
    'api_token' => env('BANDWIDTH_API_TOKEN'), 
    'api_secret' => env('BANDWIDTH_API_SECRET'), 
    'from' => env('BANDWIDTH_FROM'), 
    'dry_run' => env('BANDWIDTH_DRY_RUN'), 
],

Also, update your .env.example and .env files:

BANDWIDTH_APPLICATION_ID=
BANDWIDTH_USER_ID=
BANDWIDTH_API_TOKEN=
BANDWIDTH_API_SECRET=
BANDWIDTH_FROM=
BANDWIDTH_DRY_RUN=false
  • The from option is the phone number that your messages will be sent from.
  • The dry_run option allows to you test the channel without sending actual SMS. When dry_run is set to true, messages will be written to your application's log files instead of being sent to the recipient.

Usage

Now 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;
    
    /**
     * Get the notification channels.
     *
     * @param  mixed  $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return [BandwidthChannel::class];
    }

    /**
     * Get the text representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return BandwidthMessage|string
     */
    public function toBandwidth($notifiable)
    {
        return BandwidthMessage::create()
            ->content("Hi {$notifiable->name}, Your account was approved!");
    }
}

Add the routeNotificationForBandwidth method to your Notifiable model:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    
    /**
     * Route notifications for the bandwidth channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return array|string|boolean
     */
    public function routeNotificationForBandwidth($notification)
    {
        return $this->phone_number;
    }
}

Methods available on BandwidthMessage class

  • 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.
  • http(): Accepts an array to send along with notification http payload.

Events

  • The package utilises Laravel's inbuilt notification events
  • You can listen to these events in your app
    • Illuminate\Notifications\Events\NotificationSent
    • Illuminate\Notifications\Events\NotificationFailed

Notes (Taken from API docs)

  • The from and to numbers must be in E.164 format, for example +14244443192.
  • Message content length must be 2048 characters or less. Messages larger than 160 characters are 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 related issues, please email pro.ankurk1[at]gmail[dot]com instead of using the issue tracker.

Resources

  • Bandwidth API v2 Docs
  • Phone number validation regex

License

The MIT License.

About

Bandwidth SMS notification channel for Laravel php framework 📩

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • PHP 100.0%