princeton255/laravel-notifications-infobip

This package is abandoned and no longer maintained. No replacement package was suggested.

A custom Laravel notifications channel for Infobip SMS API

1.2.0 2020-04-07 16:16 UTC

This package is auto-updated.

Last update: 2022-10-11 12:45:41 UTC


README

Latest Stable Version License Total Downloads

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 princeton255/laravel-notifications-infobip

Setting up your Infobip account

Add your Infobip Account Username, Password, and From Number to your config/services.php:

// config/services.php
...
'infobip' => [
    'username' => env('INFOBIP_USERNAME'),
    'password' => env('INFOBIP_PASSWORD'),
    'from' => env('INFOBIP_FROM', 'IBTEST'),
],
...

To change Base URL to personal use this (See more)

...
'infobip' => [
    ...
    'baseUrl' => env('INFOBIP_BASE_URL', null),
],
...

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)
    {
        return (new InfobipMessage())
            ->content("Your {$notifiable->service} account was approved!");
    }
}

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

  • from(''): Accepts a phone to use as the notification sender.
  • content(''): Accepts a string value for the notification body.

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 NotificationChannels\Infobip\InfobipChannel;
use NotificationChannels\Infobip\InfobipMessage;

class ExampleInfobipNotification extends Notification
{
    public function via($notifiable)
    {
        return [InfobipChannel::class];
    }

    public function toInfobip($notifiable)
    {
        return (new InfobipMessage())
            // Customize your msg content here
            ->content("Your {$notifiable->service} account was approved!"); 
    }
}

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 email princeton.mosha@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT).