caherrera / laravel-notifications-infobip-omni
A custom Laravel notifications channel for Infobip API OMNI. Based on princeton255/laravel-notifications-infobip
Package info
github.com/caherrera/laravel-notifications-infobip-omni
pkg:composer/caherrera/laravel-notifications-infobip-omni
Requires
- php: ^7.1
- caherrera/infobip-api-php-client: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.13
- illuminate/events: ^7.0
- illuminate/notifications: ^7.0
- illuminate/queue: ^7.0
- illuminate/support: ^7.0
- phpunit/phpunit: ^8.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
This package makes it easy to send Sms notifications using Infobip service with Laravel 5.5 and above.
Contents
Installation
You can install the package via composer:
composer require caherrera/laravel-notifications-infobip
Setting up your Infobip account
Add this settings to config/services.php:
// config/services.php ... 'infobip' => [ 'auth' => env('INFOBIP_AUTH','basic'), 'username' => env('INFOBIP_USERNAME'), 'password' => env('INFOBIP_PASSWORD'), 'baseUrl' => env('INFOBIP_BASE_URL'), 'apikey' => env('INFOBIP_PUBLIC_KEY'), 'scenarioKey' => env('INFOBIP_SCENARIO_KEY'), ], ...
To change Base URL to personal use this (See more)
Usage
Now you can use the channel in your via() method inside the notification:
use NotificationChannels\Infobip\InfobipChannel; use NotificationChannels\Infobip\InfobipMessage; use Illuminate\Notifications\Notification; class AccountApproved extends Notification { public function via($notifiable) { return [InfobipChannel::class]; } public function toInfobip($notifiable) { $message = new InfobipMessage(); $message->setTemplateName("infobip_test_hsm"); $message->setTemplateNamespace("whatsapp:hsm:it:infobip"); $message->setTemplateData(["Jhon","Snow"]); $message->setLanguage("es"); return $message; } }
In order to let your Notification know which phone are you sending to, the channel will look for the phone_number attribute of the Notifiable model. If you want to override this behaviour, add the routeNotificationForInfobip method to your Notifiable model.
public function routeNotificationForInfobip() { return '+1234567890'; }
Available Message methods
InfobipMessage
setTemplateName(''): Accepts a string value.setTemplateNamespace(''): Accepts a string value.setTemplateData(['','',...]): Accepts an array of string.setLanguage(''): Accepts a string value
Examples
Dispatching the notification
A. Using Laravel's notification facade
use App\Notifications\ExampleInfobipNotification; use Illuminate\Support\Facades\Notification; Notification::send($user, new ExampleInfobipNotification()); // where $user implements `Illuminate\Notifications\Notifiable` trait
B. Using the notify() method from Notifiable trait
use App\Notifications\ExampleInfobipNotification; $user->notify(new ExampleInfobipNotification($invoice));
Example Notification class
<?php namespace App\Notifications; use Illuminate\Notifications\Notification; use Caherrera\Laravel\Notifications\Channels\Infobip\Omni\InfobipChannel; use Caherrera\Laravel\Notifications\Channels\Infobip\Omni\InfobipMessage; class ExampleInfobipNotification extends Notification { public function via($notifiable) { return [InfobipChannel::class]; } public function toInfobip($notifiable) { $message = new InfobipMessage(); $message->setTemplateName("infobip_test_hsm"); $message->setTemplateNamespace("whatsapp:hsm:it:infobip"); $message->setTemplateData(["Jhon","Snow"]); $message->setLanguage("es"); return $message; } }
Example Notifiable class
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; }
For more details you can check out this link on Laravel documentation
Testing
$ ./vendor/bin/phpunit
Security
If you discover any security related issues, please help me and rise a ticket on issue tracker or simply fix it and I'll merge
Credits
- Based on Twilio SMS Notification channel for Laravel
- Based on princeton255/laravel-notifications-infobip Infobip Notifications Channel for Laravel 5.5+
- This project uses the Infobip Client library, and wraps it for smooth use in Laravel
License
The MIT License (MIT).