andreshg112/aws-sns

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

v1.0.2 2023-09-25 00:38 UTC

This package is auto-updated.

Last update: 2024-09-25 03:45:08 UTC


README

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

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

内容

安装

可以使用以下命令安装

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 控制台生成的 API 密钥和 API 密钥,连接两者。

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 的 Topic Arn;
  • phoneNumber($phoneNumber):发送通知的电话号码;
  • message($message):要发送的消息;

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

测试

$ composer test

安全性

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

贡献

请参阅 CONTRIBUTING 了解详细信息。

致谢

许可

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