itsnubix/aws-sns-sms-channel

Laravel 的 AWS SNS SMS 通知通道

1.0.6 2023-10-02 16:25 UTC

This package is auto-updated.

Last update: 2024-08-31 00:35:35 UTC


README

Total Downloads Build Status Latest Stable Version Software License

安装

composer require itsnubix/aws-sns-sms-channel

在你的 config/services.php 文件中输入

'sns' => [
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('SNS_DEFAULT_REGION'),
],

请注意,区域不一定是标准的 AWS_DEFAULT_REGION,因为只有某些区域允许从这些区域发送短信。 点击这里 查看允许短信的节点列表。

确保拥有访问密钥和秘密的用户在 AWS IAM 上至少有以下策略

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowSendingSMSMessages",
      "Effect": "Allow",
      "Action": [
        "sns:Publish",
        "sns:SetSMSAttributes",
        "sns:CheckIfPhoneNumberIsOptedOut"
      ],
      "Resource": ["*"]
    }
  ]
}

现在在你的通知中,你可以执行以下操作

<?php
namespace App\Notifications;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Nubix\Notifications\Messages\SmsMessage;

class SendHelloText extends Notification
{
    use Queueable;

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     *
     * @return array
     */
    public function via($notifiable)
    {
        return ['sms'];
    }
    /**
     * Get the SMS representation of the notification.
     *
     * @param mixed $notifiable
     *
     * @return \App\Channels\Messages\SmsMessage
     */
    public function toSms($notifiable)
    {
        return (new SmsMessage())
            ->content('Hello world');
    }
}

最后,你需要扩展你的可通知类,以下函数以便它知道如何路由 SMS 通知。

<?php
namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Return the SMS notification routing information.
     *
     * @param \Illuminate\Notifications\Notification|null $notification
     *
     * @return mixed
     */
    public function routeNotificationForSms(?Notification $notification = null)
    {
        return $this->phone_number;
    }
}

需要更多帮助? 在这里阅读文章