itsnubix / aws-sns-sms-channel
AWS SNS SMS Notifications Channel for Laravel
Installs: 26 993
Dependents: 0
Suggesters: 0
Security: 0
Stars: 16
Watchers: 3
Forks: 7
Open Issues: 0
Requires
- php: ^7.1.3|^8.0
- aws/aws-sdk-php: ^3.110
- illuminate/notifications: ~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0|^8.0|^9.0
README
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