trunk-studio / aws-sns-laravel-notification
aws-sns-laravel-notification
Requires
- php: >=7.2
- illuminate/notifications: ~6.0 || ~7.0 || ~8.0
- illuminate/support: ~6.0 || ~7.0 || ~8.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-21 06:36:41 UTC
README
内容
安装
使用Composer安装此包
composer require laravel-notification-channels/aws-sns
安装包后,请在config/app.php中注册ServiceProvider。
NotificationChannels\AwsSns\SNSServiceProvider::class
设置AWS SNS服务
您需要从AWS控制台获取所需的API访问密钥和密钥。同时,您需要定义用于SNS的分区。
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_REGION (default = us-east-1)
如果您需要覆盖其他AWS API访问设置,请参阅此包所依赖的官方AWS PHP Laravel Service Provider的配置部分。
用法
为了通过SNS发送通知,您需要创建一个通知类。如果您想向特定端点发送消息,则需要提供TargetArn。如果您想向主题发送,则提供TopicArn。一个简单的示例如下
use Illuminate\Notifications\Notification;
use NotificationChannels\AwsSns\SNSMessage;
use NotificationChannels\AwsSns\SNSChannel;
use NotificationChannels\AwsSns\Notifications\APNS;
use NotificationChannels\AwsSns\Notifications\GCM;
use NotificationChannels\AwsSns\Notifications\SMS;
class AccountApproved extends Notification
{
public function via($notifiable)
{
return [SNSChannel::class];
}
public function toSNS($notifiable)
{
return (new SNSMessage)
->message('This is a simple message')
->subject('This is subject')
->topicArn('<YourTopicArn>');
}
}
SNS支持为特定端点自定义消息。因此,您可以为各种平台设置自定义消息。例如,您可以为通过GCM注册的主题订阅者发送的自定义GCM推送通知进行配置。目前,已实现APNS、GCM和SMS。其他由AWS支持的端点将在以后添加。
public function toSNS($notifiable)
{
return (new SNSMessage())
->message('This is a custom message')
->subject('This is subject')
->messageStructure('json')
->topicArn('<TopicArn>')
->apnsMessage((new APNS)
->message('APNS custom message')
->badge(2)
->sound('sound.mp3')
->addCustomPayload('custom1', 'test value 1')
->addCustomPayload('custom2', 'test value 2'))
->gcmMessage((new GCM)
->message('GCM custom message'))
->smsMessage(new SMS('SMS custom message'));
}
可用的消息方法
通用SNS通知的方法
subject()
:通知的主题。message()
:通知的默认消息。topicArn()
:要发送通知的主题。targetArn()
:特定的目标ARN,如果您想向特定订阅者发送通知。messageStructure()
:如果您想为不同的端点使用自定义通知,则需要将消息结构设置为'json'
。messageAttributes()
:AWS SNS支持的自定义属性。phoneNumber()
:如果您想发送短信,请设置电话号码。apnsMessage()
:自定义APNS消息。gcmMessage()
:自定义GCM消息。smsMessage()
:自定义SMS消息。
APNS通知的方法
message()
:APNS自定义消息。badge()
:应用图标徽章计数。sound()
:警报消息声音。addCustomPayload()
:通知的自定义有效负载。
GCM通知的方法
message()
:GCM自定义消息。data()
:数据有效负载。notification()
:通知有效负载。priority()
:通知优先级。collapseKey()
:通知合并密钥。timeToLive()
:通知生存时间。
SMS通知的方法
message()
:SMS自定义消息。
有关每个方法的详细信息,请参阅SNS PHP SDK。
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
测试
$ composer test
安全
如果您发现任何与安全相关的问题,请通过电子邮件service@trunk-studio.com联系,而不是使用问题跟踪器。
贡献
有关详细信息,请参阅CONTRIBUTING。
致谢
许可协议
麻省理工学院许可证(MIT)。有关更多信息,请参阅许可证文件。