cuongnd88/delivery-channels

There is no license information available for the latest version (1.1) of this package.

Laravel Notification Delivery Channels

1.1 2021-06-14 08:19 UTC

This package is auto-updated.

Last update: 2024-04-14 16:35:17 UTC


README

Laravel ships with a handful of notification channels, but you may want to more drivers to deliver notifications via other channels. The delivery-channels makes it simple.

The package currently supports these drivers:

  • Twilio

1-Install cuongnd88/delivery-channels using Composer.

$ composer require cuongnd88/delivery-channels

2-Add the following service provider in config/app.php

<?php
// config/app.php
return [
    // ...
    'providers' => [
        // ...
        Cuongnd88\DeliveryChannel\DeliveryChannelServiceProvider::class,
    ]
    // ...
];

For further configurations, you can modify the configuration by copying it to your local config directory:

php artisan vendor:publish --provider="Cuongnd88\DeliveryChannel\DeliveryChannelServiceProvider" --tag=config

3-Update credentails in config/channels.php or env file

<?php

return [
    'twilio' => [
        'account_sid' => env('TWILIO_ACCOUNT_SID'),
        'auth_token' => env('TWILIO_AUTH_TOKEN'),
        'sms_from' => env('TWILIO_SMS_FROM'),
    ],
];

Sample Usage

In the Laravel notification class, it contains a via method and a variable number of message building methods (such as toMail or toDatabase) that convert the notification to a message optimized for that particular channel.

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['mail', 'twilio'];
}

Twilio Channel

You have to define a toTwilio method on the notification class. This method will receive a $notifiable entity and should return a Cuongnd88\DeliveryChannel\Messages\TwilioMessage instance or array. Let's take a look at an example toTwilio method:

    /**
     * Get the Twilio / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     *
     * @return mixed
     */
    public function toTwilio($notifiable)
    {
        return (new TwilioMessage)
                    ->to("+xxxxxx")
                    ->from("+xxxxx")
                    ->body('OTP AUTH is '.$this->otp);
    }
    /**
     * Get the Twilio / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     *
     * @return mixed
     */
    public function toTwilio($notifiable)
    {
        return [
            'to' => "+84xxxxxxxxxx",
            'body' => 'OTP AUTH is '.$this->otp
        ];           ->body('OTP AUTH is '.$this->otp);
    }

Demo

This is demo soure code. Laravel Colab