(We forked this because it's no longer being updated by the developer and we might want to make changes to it.)
This package makes it easy to send Bandwidth SMS notifications with Laravel v7.0+
You can install the package via composer:
composer require ankurk91/bandwidth-notification-channel
Package will auto register the service provider.
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
fromoption is the phone number that your messages will be sent from. - The
dry_runoption allows to you test the channel without sending actual SMS. Whendry_runis set totrue, messages will be written to your application's log files instead of being sent to the recipient.
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;
}
}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 anarrayto send along with notification http payload.
- The package utilises Laravel's inbuilt notification events
- You can listen to these events in your app
Illuminate\Notifications\Events\NotificationSentIlluminate\Notifications\Events\NotificationFailed
- The
fromandtonumbers must be inE.164format, for example+14244443192. - Message content length must be
2048characters or less. Messages larger than160characters are automatically fragmented and re-assembled to fit within the160character transport constraints.
Please see CHANGELOG for more information what has changed recently.
composer test
If you discover any security related issues, please email pro.ankurk1[at]gmail[dot]com instead of using the issue tracker.
The MIT License.