oscarcpv / aws-pinpoint-laravel-notification-channel

此包可轻松使用 Laravel 和 AWS Pinpoint 发送通知。

v1.0.0 2023-10-27 00:09 UTC

This package is auto-updated.

Last update: 2024-09-27 05:00:18 UTC


README

此包可轻松使用 AWS Pinpoint 通过 Laravel 发送通知。

目前仅支持短信。然而,任何有助于支持其他类型消息的贡献都受欢迎。

内容

安装

使用 composer 安装包

composer require oscarcpv/aws-pinpoint-laravel-notification-channel

设置 AWS Pinpoint 服务

首先,您需要将凭证添加到 config/services.php 文件中

<?php

return [
    
    //...

    'aws_pinpoint' => [
        'region' => env('AWS_PINPOINT_REGION', 'us-east-1'),
        'key' => env('AWS_PINPOINT_KEY'),
        'secret' => env('AWS_PINPOINT_SECRET'),
        'application_id' => env('AWS_PINPOINT_APPLICATION_ID'),
        'sms' => [
            'sender_id' => env('AWS_PINPOINT_SMS_SENDER_ID')
        ],
    ],
];

现在,您需要在您的 .env 文件中添加以下条目

AWS_PINPOINT_REGION=your-aws-region
AWS_PINPOINT_KEY=your-aws-pinpoint-key
AWS_PINPOINT_SECRET=your-aws-pinpoint-secret
AWS_PINPOINT_APPLICATION_ID=your-aws-pinpoint-application-id
AWS_PINPOINT_SMS_SENDER_ID=your-sms-sender-id

用法

在您的通知类中,您必须在 via 方法中包含通道

use NotificationChannels\AwsPinpointChannel;

/**
 * Get the notification's delivery channels.
 *
 * @return array<int, string>
 */
public function via($notifiable)
{
    return ['broadcast', AwsPinpointChannel:class];
} 

然后,添加 toAwsPinpoint 方法

use NotificationChannels\AwsPinpointMessage;

/**
 * Send SMS via AWS Pinpoint
 */
public function toAwsPinpoint($notifiable)
{
    return (new AwsPinpointMessage)
        ->body('Something cool')
        ->senderId('My Company')
        ->recipients($notifiable->phone)
        ->promotional();
} 

您还可以在具有 Notifiable 特性的模型中定义 routeNotificationForAwsPinpoint 方法以定义接收者或接收者

use Illuminate\Notifications\Notifiable;

class User extends Model
{
    use Notifiable;

    /**
     * Route notifications for the AWS Pinpoint channel.
     *
     * @return array|string|int
     */
    public function routeNotificationForAWSPinpoint()
    {
        return $this->phone;
    }
}

然后,您可以按如下方式定义您的 toAwsPinpoint 方法

use NotificationChannels\AwsPinpointMessage;

/**
 * Send SMS via AWS Pinpoint
 */
public function toAwsPinpoint($notifiable)
{
    return new AwsPinpointMessage("Something cool");
} 

您有这些事件可供使用,您可以自由实现监听器

  • NotificationChannels\AwsPinpoint\Events\DeliverySuccessfull:在消息成功发送后触发。
  • NotificationChannels\AwsPinpoint\Events\DeliveryFailed:如果消息无法成功发送,将触发。

这两个事件都接收以下参数

  • mixed $recipient:消息发送到的接收者。此参数可以是字符串、数组或整数。
  • mixed $deliveryStatus:AWS Pinpoint 返回的交付状态。此参数是表示消息交付状态的字符串。有关此参数的更多详细信息,请参阅 AWS Pinpoint 文档
  • mixed $statusMessage:AWS Pinpoint 返回的状态消息。此参数是包含描述消息交付状态的字符串。有关此参数的更多详细信息,请参阅 AWS Pinpoint 文档

可用的消息方法

当您实例化 NotificationChannels\AwsPinpoint\AwsPinpointMessage 时,您可以使用以下方法

  • body(string $body):设置消息正文
  • recipients(mixed $recipients):设置要发送消息的接收者。此方法接受类型为字符串或整数的接收者,或接收者数组。
  • transactional():将消息类型设置为 TRANSACTIONAL。
  • promotional():将消息类型设置为 PROMOTIONAL。
  • senderId(string $senderId):替换 config/services.php 中配置的默认发送者 ID。

有关这些概念的更多详细信息,请参阅 AWS Pinpoint 文档

变更日志

请参阅 变更日志 以获取有关最近更改的更多信息。

测试

$ composer test

安全

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

贡献

请参阅CONTRIBUTING以获取详细信息。

致谢

许可

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。