lavatech / aws-sns
Amazon Simple Notification Service (AWS SNS) 通知通道,适用于Laravel。
1.7.0
2023-12-19 15:26 UTC
Requires
- php: >=7.2|8.0
- aws/aws-sdk-php: ^3.0
- illuminate/notifications: ~5.5 || ~6.0 || ~7.0 || ~8.0 || ~9.0 || ~10.0
- illuminate/support: ~5.5 || ~6.0 || ~7.0 || ~8.0 || ~9.0 || ~10.0
Requires (Dev)
- mockery/mockery: ^1.5
- phpunit/phpunit: ^9.0
README
此包使得使用Laravel框架通过AWS SNS发送通知变得简单。由于Laravel已经包含了SES电子邮件支持,因此此包目前专注于发送短信通知。未来可能会添加更多高级功能,例如支持主题。
内容
安装
您可以通过composer安装此包
composer require lavatech/aws-sns
配置AWS SNS服务
将您的AWS密钥ID、密钥和默认区域添加到您的 config/services.php
<?php
return [
// ...
'sns' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'version' => 'latest',
],
];
使用方法
现在您可以在通知中的 via()
方法中使用该通道
<?php
use Lavatech\AwsSns\SnsChannel;
use Lavatech\AwsSns\SnsMessage;
use Illuminate\Notifications\Notification;
class AccountApproved extends Notification
{
public function via($notifiable)
{
return [SnsChannel::class];
}
public function toSns($notifiable)
{
// You can just return a plain string:
return "Your {$notifiable->service} account was approved!";
// OR explicitly return a SnsMessage object passing the message body:
return new SnsMessage("Your {$notifiable->service} account was approved!");
// OR return a SnsMessage passing the arguments via `create()` or `__construct()`:
return SnsMessage::create([
'body' => "Your {$notifiable->service} account was approved!",
'transactional' => true,
'sender' => 'MyBusiness',
]);
// OR create the object with or without arguments and then use the fluent API:
return SnsMessage::create()
->body("Your {$notifiable->service} account was approved!")
->promotional()
->sender('MyBusiness');
}
}
为了让您的通知知道您发送给哪个电话,该通道将查找Notifiable模型中的 phone
、phone_number
或 full_phone
属性。如果您想覆盖此行为,请将 routeNotificationForSns
方法添加到您的Notifiable模型中。
<?php
use Illuminate\Notifications\Notifiable;
class SomeModel {
use Notifiable;
public function routeNotificationForSns($notification)
{
return '+1234567890';
}
}
可用的SnsMessage方法
create([])
: 接受一个键值数组,键对应以下方法,值作为参数传递;body('')
: 接受一个字符串值作为通知正文。超过140个字符的消息将被SNS分割成多个消息,而不会断开任何单词;promotional(bool)
: 将投递类型设置为促销(默认)。优化投递以降低成本;transactional(bool)
: 将投递类型设置为事务性。优化投递以实现最高可靠性(这也成本更高);sender(string)
: 最多11个字符,不能有空格,在接收设备上显示为发送者。 支持因国家而异。
有关SMS属性的更多信息,请参阅AWS SNS文档。重要的是要知道,消息上设置的属性将覆盖您在AWS账户中配置的默认属性。
变更日志
请参阅 CHANGELOG 了解最近的变化。
测试
$ composer test
安全
如果您发现任何与安全相关的问题,请通过电子邮件claudson@outlook.com联系,而不是使用问题跟踪器。
贡献
有关详细信息,请参阅 CONTRIBUTING。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅 许可证文件。