niisan/laravel-sms-notification

Add SMS notification channel to laravel.

v0.2.1 2021-01-11 15:11 UTC

This package is auto-updated.

Last update: 2024-04-11 22:24:51 UTC


README

Add SMS notification channnel to laravel.

Install

Fisrt, install package via composer.

composer require niisan/laravel-sms-notification

And install using client sdk. If we use twilio, we install twilio sdk.

composer require twilio/sdk

Then we publish configuration.

php artisan vendor:publish --provider "Niisan\Notification\SmsNotificationServiceProvider"

Usage

Set .env.

SMS_DRIVER=twilio
SMS_DEFAULT_FROM='+1111222233'
TWILIO_SID=***********************
TWILIO_TOKEN=***********************

In notification class, we can write followin:

    public function via($notifiable)
    {
        return ['sms'];
    }

    public function toSms($notifiable)
    {
        return (new SmsMessage)
                ->body('hello world! ' . $notifiable->name);
    }

And in notifiable model, we get phone_number from routeNotificationForSms method to send messages:

    public function routeNotificationForSms()
    {
        return $this->phone_number;
    }

In this case, SMS sent from the env value: SMS_DEFAULT_FROM.

Otherwise, set to and from by SmsMessage class:

    public function toSms($notifiable)
    {
        return (new SmsMessage)
                ->body('hello world! ' . $notifiable->name)
                ->to('+2222333344')
                ->from('+1111222233');
    }

Unicode

if we want to use unicode string, we use unicode method:

    public function toSms($notifiable)
    {
        return (new SmsMessage)
                ->body('hello world! ' . $notifiable->name)
                ->unicode();
    }