Amazon Simple Notification Service (AWS SNS) 的 Laravel 通知通道。

1.6.0 2024-03-12 22:13 UTC

This package is auto-updated.

Last update: 2024-09-12 23:15:51 UTC


README

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

此包使您能够通过 Laravel 框架轻松使用 AWS SNS 发送通知。由于 Laravel 已经包含 SES 邮件支持,因此此包目前专注于仅发送 SMS 通知。未来可能会添加更多高级功能,例如对主题的支持。

内容

安装

您可以通过 composer 安装此包

composer require laravel-notification-channels/aws-sns --update-with-dependencies

设置 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'),
    ],

];

用法

现在您可以在通知中的 via() 方法中使用该通道

<?php

use NotificationChannels\AwsSns\SnsChannel;
use NotificationChannels\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');
    }
}

为了使通知知道您发送到哪个手机,该通道将查找可通知模型的 phonephone_numberfull_phone 属性。如果您想覆盖此行为,请在可通知模型中添加 routeNotificationForSns 方法。

<?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 个字符,无空格,将显示为接收设备上的发送者。 支持因国家/地区而异
  • originationNumber(string): 识别 SMS 消息发送者电话号码的数字字符串。在某些国家/地区可能不可用,请参阅 AWS SNS 原始号码文档

有关 SMS 属性的更多信息,请参阅 AWS SNS 文档。重要的是要知道,在消息上设置的属性将覆盖在您的 AWS 账户中配置的默认属性。

常见问题

异常处理

为了给其他通道一个正常工作的机会,包不会抛出异常。相反,将触发一个 Illuminate\Notifications\Events\NotificationFailed 事件。为了调试目的,您可以在 EventServiceProvider.phpboot 方法中监听此事件。

Event::listen(function (\Illuminate\Notifications\Events\NotificationFailed $event) {
    //Dump and die
    dd($event);
    
    //or log the event
    Log::error('SNS error', $event->data)
});

AWS 的权限不足

默认情况下,Laravel Vapor 在 AWS 中创建名为 laravel-vapor-role 的角色,该角色没有发送 SMS 通过 SNS 的权限。这导致在本地环境中 SMS 可以成功发送,但在 Vapor 环境中则无法发送。如果您使用的是无法与 SNS 交互的特定 IAM 用户 ID/Secret 对,您的消息也可能无法发送。

无论哪种情况,您都需要确保您使用的凭证(无论是您的角色还是用户)有权与您的应用程序需要的 AWS 服务交互。在 IAM(身份和访问管理)中,您可以将 AWS 管理策略 AmazonSNSFullAccess 或更细粒度的自定义策略附加到您的应用程序使用的角色/用户。

更新日志

有关最近更改的更多信息,请参阅 变更日志

测试

$ composer test

安全

如果您发现任何与安全相关的问题,请发送电子邮件至 claudson@outlook.com,而不是使用问题跟踪器。

贡献

有关详细信息,请参阅 贡献指南

致谢

许可

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