friendsofhyperf/notification-easysms

The EasySms Channel for friendsofhyperf/notification.

v3.1.43 2024-10-14 06:11 UTC

This package is auto-updated.

Last update: 2024-10-18 01:35:56 UTC


README

Latest Version on Packagist Total Downloads GitHub license

Installation

composer require friendsofhyperf/notification-easysms:~3.1.0

Usage

Use Notifiable trait in Model

<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;
use FriendsOfHyperf\Notification\Traits\Notifiable;
use Overtrue\EasySms\PhoneNumber;

/**
 * @property int $id 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 */
class User extends Model
{
    use Notifiable;

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'user';

    // 通知手机号
    public function routeNotificationForSms(): string|PhoneNumber
    {
        return $this->phone;
    }
}

SMS Notifications

  • Install the easy-sms package
composer require overtrue/easy-sms:^3.0
namespace App\Notification;

use FriendsOfHyperf\Notification\EasySms\Contract\EasySmsChannelToSmsArrayContract;
use FriendsOfHyperf\Notification\EasySms\Contract\Smsable;
use FriendsOfHyperf\Notification\Notification;
use Overtrue\EasySms\Message;

// 通知类
class TestNotification extends Notification implements Smsable
{
    public function __construct(private string $code)
    {
    }
    
    public function via()
    {
        return [
            'easy-sms'
        ];
    }
    
    /**
     * 返回的内容将组装到短信模型中 new Message($notification->toSms()). 
     * 文档 https://github.com/overtrue/easy-sms?tab=readme-ov-file#%E5%AE%9A%E4%B9%89%E7%9F%AD%E4%BF%A1 
     */
    public function toSms(mixed $notifiable): array|Message
    {
        return [
            'code' => $this->code,
            'template' => 'SMS_123456789',
            'data' => [
                'code' => $this->code,
            ]
        ];
    }

    // or return customer Message
    // public function toSms(mixed $notifiable): array|Message
    // {
    //     return new Message();
    // }

}