PHP code example of hahadu / laravel-notfication-sms

1. Go to this page and download the library: Download hahadu/laravel-notfication-sms library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hahadu / laravel-notfication-sms example snippets




namespace App\Notifications;

use Hahadu\LaravelSms\SmsNotification;
use Hahadu\LaravelSms\Message\SmsMessage;

class VerificationCodeNotification extends SmsNotification
{
    private $code;

    public function __construct(string $code)
    {
        // 传入模板参数和模板代码
        parent::__construct(
            ['code' => $code],  // 模板参数
            'SMS_TEMPLATE_CODE' // 模板代码
        );

        $this->code = $code;
    }

    // 可选:自定义短信内容
    public function toSms($notifiable): SmsMessage
    {
        return (new SmsMessage())
            ->content(['code' => $this->code])
            ->template('SMS_TEMPLATE_CODE')
            ->from('YourApp'); // 可选:自定义签名
    }
}



namespace App\Models;

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

class User extends Authenticatable
{
    use Notifiable;

    // 添加路由方法指定接收短信的手机号
    public function routeNotificationForSms($notification)
    {
        return $this->phone; // 返回手机号字段
    }
}

// 方式1:直接发送
$user->notify(new VerificationCodeNotification('123456'));

// 方式2:延迟发送
$user->notify((new VerificationCodeNotification('123456'))->delay(now()->addMinutes(10)));

use Hahadu\LaravelSms\SmsNotification;
use Hahadu\LaravelSms\Message\SmsMessage;

class OrderShippedNotification extends SmsNotification
{
    protected $order;

    public function __construct($order)
    {
        parent::__construct(
            [
                'order_no' => $order->number,
                'shipping_company' => $order->shipping_company,
                'tracking_number' => $order->tracking_number
            ],
            'SMS_ORDER_SHIPPED'
        );

        $this->order = $order;
    }

    public function toSms($notifiable): SmsMessage
    {
        return (new SmsMessage())
            ->content([
                'order_no' => $this->order->number,
                'shipping_company' => $this->order->shipping_company,
                'tracking_number' => $this->order->tracking_number
            ])
            ->template('SMS_ORDER_SHIPPED')
            ->statusCallback(function ($result) {
                // 发送成功的回调
                Log::info('SMS sent successfully', [
                    'order_id' => $this->order->id,
                    'result' => $result
                ]);
            })
            ->errorCallback(function ($exception) {
                // 发送失败的回调
                Log::error('SMS sending failed', [
                    'order_id' => $this->order->id,
                    'error' => $exception->getMessage()
                ]);
            });
    }
}

use Illuminate\Support\Facades\Notification;

// 发送给多个用户
$users = User::whereIn('id', [1, 2, 3])->get();
Notification::send($users, new VerificationCodeNotification('123456'));

use Hahadu\LaravelSms\Client\SmsClient;

$smsClient = new SmsClient();
$smsClient->set_accessKey('new_access_key')
         ->set_secret('new_secret')
         ->set_signName('new_sign_name')
         ->set_service('Aliyun');

// 在发送短信时使用自定义客户端
(new SmsMessage())
    ->client($smsClient)
    ->content(['code' => '123456'])
    ->template('SMS_TEMPLATE_CODE');

try {
    $user->notify(new VerificationCodeNotification('123456'));
} catch (\Exception $e) {
    // 处理发送失败
    Log::error('SMS sending failed', [
        'error' => $e->getMessage(),
        'user_id' => $user->id
    ]);
}

// 创建测试路由
Route::get('/test/sms', function () {
    $user = App\Models\User::find(1);
    $user->notify(new VerificationCodeNotification('123456'));
    return 'SMS sent';
});