Amazon SNS Notifications Channel for Laravel 5.3 [WIP]
, (*1)
This package makes it easy to send notifications using Amazon SNS with Laravel 5.3., (*2)
Contents
Installation
Can to install with commands:, (*3)
composer require lab123/aws-sns:dev-master
Or editing the composer.json file:, (*4)
"require": {
"lab123/aws-sns": "dev-master"
}
You must install the service provider., (*5)
Laravel 5.x, (*6)
configure in config/app.php:, (*7)
'providers' => [
...
Lab123\AwsSns\AwsSnsServiceProvider::class,
]
Lumen 5.x, (*8)
configure in bootstrap/app.php:, (*9)
$app->register(\Lab123\AwsSns\AwsSnsLumenServiceProvider::class);
** Note: ** You go need make a wrapper Laravel Notification or use a package how
Lumen Notification, (*10)
Setting up the AwsSns service
Follow the Amazon Console generate the APIKEY and API SECRET, which is connecting both., (*11)
Create a new sns section inside config/services.php:, (*12)
...
'sns' => [
'key' => env('SNS_KEY'),
'secret' => env('SNS_SECRET'),
'region' => env('SNS_REGION', 'us-east-1')
],
...
Next we need to add this keys to our Laravel environment. Edit file .env to config the keys:, (*13)
...
SNS_KEY=YOUR_KEY
SNS_SECRET=YOUR_SECRET
SNS_REGION=YOUR_REGION
...
Default Configurations SMS (OPTIONAL)
You also can config defaults attributes for sending SMS running php artisan vendor:publish --provider="Lab123\AwsSns\AwsSnsServiceProvider" or creating file config/aws-sns.php:, (*14)
return [
'sms' => [
'monthlySpendLimit' => env('SNS_SMS_MONTHLY_LIMIT'),
'deliveryStatusIAMRole' => env('SNS_SMS_DELIVERY_STATUS_IAM_ROLE'),
'deliveryStatusSuccessSamplingRate' => env('SNS_SMS_DELIVERY_STATUS'),
'defaultSenderID' => env('SNS_SMS_SENDER'),
'defaultSMSType' => env('SNS_SMS_TYPE'),
'usageReportS3Bucket' => env('SNS_SMS_REPORT_S3')
]
]
And now you can set your default configuration for SMS in .env, (*15)
** Note: ** More information how they work the settings at http://docs.aws.amazon.com/en/sns/latest/api/API_SetSMSAttributes.html, (*16)
Usage
Sending SMS
To send sms without the need to create a topic, leave the function via as follows:, (*17)
// Notifications/Welcome.php
/**
* Get the notification channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return [
AwsSnsSmsChannel::class
];
}
Add function toAwsSnsSms() expected by class AwsSnsSmsChannel to send notification:, (*18)
// Notifications/Welcome.php
/**
* Get the AWS SNS SMS Message representation of the notification.
*
* @param mixed $notifiable
* @return \Lab123\AwsSns\Messages\AwsSnsMessage
*/
public function toAwsSnsSms($notifiable)
{
return (new AwsSnsMessage())->message('Message Here')->phoneNumber('+5511999999999');
}
You also can ignore the ->phoneNumber() in your notification and use function routeNotificationForAwsSnsSms in your Model Notifiable:, (*19)
// Models/User.php
/**
* Route notifications for the Aws SNS SMS channel.
*
* @return string
*/
public function routeNotificationForAwsSnsSms()
{
return $this->phone_number;
}
**Note.: ** The expected number use the standards-based international E.123, (*20)
eg.: +5511999999999, (*21)
Sending Topic
To send notification to a topic, leave the function via as follows:, (*22)
// Notifications/Welcome.php
/**
* Get the notification channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return [
AwsSnsTopicChannel::class
];
}
Add function toAwsSnsTopic() expected by class AwsSnsTopicChannel to send notification:, (*23)
// Notifications/Welcome.php
/**
* Get the AWS SNS Topic Message representation of the notification.
*
* @param mixed $notifiable
* @return \Lab123\AwsSns\Messages\AwsSnsMessage
*/
public function toAwsSnsTopic($notifiable)
{
return (new AwsSnsMessage())->message('Message Here')->topicArn('arn:aws:sns:us-east-1:000000000000:name-topic');
}
You also can ignore the ->topicArn() in your notification and use function routeNotificationForAwsSnsTopic in your Model Notifiable:, (*24)
// Models/User.php
/**
* Route notifications for the Aws SNS Topic channel.
*
* @return string
*/
public function routeNotificationForAwsSnsTopic()
{
return $this->topicArn;
}
Available methods
-
topicArn($topicArn): Your Topic Arn from Amazon SNS;
-
phoneNumber($phoneNumber): Phone number to send notification;
-
message($message): Message to be sent;
Changelog
Please see CHANGELOG for more information what has changed recently., (*25)
Testing
bash
$ composer test, (*26)
Security
If you discover any security related issues, please email jean.pierre@lab123.com.br instead of using the issue tracker., (*27)
Contributing
Please see CONTRIBUTING for details., (*28)
Credits
License
The MIT License (MIT). Please see License File for more information., (*29)