lab123/aws-sns

为 Laravel 5.3 提供的 Amazon SNS 通知通道

dev-master 2017-09-13 02:57 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:25:54 UTC


README

Latest Version on Packagist Software License Build Status Quality Score Code Coverage Total Downloads

此包简化了使用 Amazon SNS 在 Laravel 5.3 中发送通知的过程。

内容

安装

可以使用以下命令安装

composer require lab123/aws-sns:dev-master

或编辑 composer.json 文件

"require": {
	"lab123/aws-sns": "dev-master"
}

您必须安装服务提供者。

Laravel 5.x

config/app.php 中配置

'providers' => [
    ...
    Lab123\AwsSns\AwsSnsServiceProvider::class,
]

Lumen 5.x

bootstrap/app.php 中配置

$app->register(\Lab123\AwsSns\AwsSnsLumenServiceProvider::class);

**注意**:您需要创建一个包装 Laravel Notification 或使用一个包如 Lumen Notification

设置 AwsSns 服务

按照 Amazon 控制台生成的 APIKEY 和 API SECRET 进行配置,这将连接两者。

config/services.php 中创建一个新的 sns 部分

...
'sns' => [
    'key' => env('SNS_KEY'),
    'secret' => env('SNS_SECRET'),
    'region' => env('SNS_REGION', 'us-east-1')
],
...

接下来,我们需要将这些密钥添加到我们的 Laravel 环境中。编辑 .env 文件以配置密钥

...
SNS_KEY=YOUR_KEY
SNS_SECRET=YOUR_SECRET
SNS_REGION=YOUR_REGION
...

默认配置 SMS(可选)

您还可以通过运行 php artisan vendor:publish --provider="Lab123\AwsSns\AwsSnsServiceProvider" 或创建 config/aws-sns.php 文件来配置发送 SMS 运行的默认属性

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')
    ]
]

现在您可以在 .env 中设置您的 SMS 默认配置

**注意**:有关设置如何工作的更多信息,请参阅 http://docs.aws.amazon.com/en/sns/latest/api/API_SetSMSAttributes.html

用法

发送短信

如果您不需要创建主题即可发送短信,请将 function via 设置如下

// Notifications/Welcome.php
/**
 * Get the notification channels.
 *
 * @param mixed $notifiable            
 * @return array|string
 */
public function via($notifiable)
{
    return [
        AwsSnsSmsChannel::class
    ];
}

添加类 AwsSnsSmsChannel 预期用于发送通知的函数 toAwsSnsSms()

// 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');
}

您还可以在通知中忽略 ->phoneNumber() 并在您的 Model Notifiable 中使用函数 routeNotificationForAwsSnsSms

// Models/User.php
/**
 * Route notifications for the Aws SNS SMS channel.
 *
 * @return string
 */
public function routeNotificationForAwsSnsSms()
{
    return $this->phone_number;
}

**注意**:预期号码使用基于标准的国际 E.123

例如。 +5511999999999

发送主题

要向主题发送通知,请将 function via 设置如下

// Notifications/Welcome.php
/**
 * Get the notification channels.
 *
 * @param mixed $notifiable            
 * @return array|string
 */
public function via($notifiable)
{
    return [
        AwsSnsTopicChannel::class
    ];
}

添加类 AwsSnsTopicChannel 预期用于发送通知的函数 toAwsSnsTopic()

// 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');
}

您还可以在通知中忽略 ->topicArn() 并在您的 Model Notifiable 中使用函数 routeNotificationForAwsSnsTopic

// Models/User.php
/**
 * Route notifications for the Aws SNS Topic channel.
 *
 * @return string
 */
public function routeNotificationForAwsSnsTopic()
{
    return $this->topicArn;
}

可用方法

  • topicArn($topicArn):您的 Amazon SNS 主题 Arn;
  • phoneNumber($phoneNumber):发送通知的电话号码;
  • message($message):要发送的消息;

更新日志

有关最近更改的更多信息,请参阅 CHANGELOG

测试

$ composer test

安全性

如果您发现任何安全相关的问题,请通过电子邮件 jean.pierre@lab123.com.br 而不是使用问题跟踪器。

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件