ayles-software/laravel-sms-messagemedia

ClickSend 通知通道,适用于 Laravel 10

2.1.0 2024-01-10 22:44 UTC

This package is auto-updated.

Last update: 2024-09-11 00:06:59 UTC


README

此包使用 messagemedia.com 和 Laravel 10 简化发送通知的过程。

安装

通过 composer 安装此包

composer require ayles-software/laravel-sms-messagemedia

在 Message Media 中生成 API 密钥时,请确保使用基本认证类型。 不支持 HMAC 认证

将您的 MessageMedia API 密钥、密钥和可选的默认发送者 sms_from 添加到 config/services.php

'message_media' => [
    'key' => env('MESSAGE_MEDIA_KEY'),
    'secret'  => env('MESSAGE_MEDIA_SECRET'),
    'from' => env('MESSAGE_MEDIA_FROM'),
],

用法

在通知类中的 via() 方法中使用 MessageMediaChannel。 示例

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use AylesSoftware\MessageMedia\MessageMediaChannel;
use AylesSoftware\MessageMedia\MessageMediaMessage;

class SmsTest extends Notification
{
    public function __construct(public string $token)
    {
    }

    public function via($notifiable)
    {
        return [MessageMediaChannel::class];
    }

    public function toMessageMedia($notifiable)
    {
        return (new MessageMediaMessage)
            ->message("SMS test to user #{$notifiable->id} with token {$this->token} by MessageMedia")
            ->from('Dory');
            
        // setting a delay when the sms should be sent
        return (new MessageMediaMessage)
            ->message("SMS test to user #{$notifiable->id} with token {$this->token} by MessageMedia")
            ->from('Dory')
            ->delay(now()->addHours(6));
    }
}

在可通知模型(User)中包含方法 routeNotificationForMessageMedia(),该方法返回接收者的手机号码

public function routeNotificationForMessageMedia()
{
    return $this->phone;
}

然后以标准方式发送通知

$user = User::find(1);

$user->notify(new SmsTest);

事件

以下事件由 Notification 触发。默认情况下

  • Illuminate\Notifications\Events\NotificationSending
  • Illuminate\Notifications\Events\NotificationSent

如果发生错误,将触发 NotificationFailed

  • Illuminate\Notifications\Events\NotificationFailed

测试

许可

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