Skip to content

Latest commit

 

History

History
113 lines (94 loc) · 3.09 KB

README.md

File metadata and controls

113 lines (94 loc) · 3.09 KB

AWS SNS SMS Notifications Channel for Laravel

Total Downloads Build Status Latest Stable Version Software License

Installation

composer require itsnubix/aws-sns-sms-channel

In your config/services.php file enter:

'sns' => [
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('SNS_DEFAULT_REGION'),
],

Notice here that the region is not necessarily your standard AWS_DEFAULT_REGION as only certain regions allow SMS messages to be sent from them. Click here for a list of nodes that allow SMS.

Be sure that the user who owns your access key and secret has at least the following policy on AWS IAM:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowSendingSMSMessages",
      "Effect": "Allow",
      "Action": [
        "sns:Publish",
        "sns:SetSMSAttributes",
        "sns:CheckIfPhoneNumberIsOptedOut"
      ],
      "Resource": ["*"]
    }
  ]
}

Now in your notifications you can do the following:

<?php
namespace App\Notifications;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Nubix\Notifications\Messages\SmsMessage;

class SendHelloText extends Notification
{
    use Queueable;

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     *
     * @return array
     */
    public function via($notifiable)
    {
        return ['sms'];
    }
    /**
     * Get the SMS representation of the notification.
     *
     * @param mixed $notifiable
     *
     * @return \App\Channels\Messages\SmsMessage
     */
    public function toSms($notifiable)
    {
        return (new SmsMessage())
            ->content('Hello world');
    }
}

Finally you'll need to extend your notifiable class with the following function so it knows how to route SMS notifications.

<?php
namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Return the SMS notification routing information.
     *
     * @param \Illuminate\Notifications\Notification|null $notification
     *
     * @return mixed
     */
    public function routeNotificationForSms(?Notification $notification = null)
    {
        return $this->phone_number;
    }
}

Need more help? Read the article here