va/smart-sms

一个用于在Laravel应用程序中发送短信通知的包。

v1.0.2 2020-11-22 22:05 UTC

This package is auto-updated.

Last update: 2024-09-26 13:52:27 UTC


README

GitHub issues GitHub stars GitHub forks GitHub license

smartsms.ir 提供的短信通知服务和通道,用于通过 smartsms.ir 发送短信。

如何安装和配置 va/smart-sms 包?

安装


composer require va/smart-sms

发布配置文件


php artisan vendor:publish --tag=smart-sms

配置文件在哪里?在 config/smart-sms.php


'line_number' => env('SMS_LINE_NUMBER'),
'user_id' => env('SMS_USER_ID'),
'password' => env('SMS_PASSWORD'),
'default_sms_rcpt' => env('SMS_DEFAULT_SMS_RCPT'),
'mobile_field_name' => env('SMS_MOBILE_FIELD_NAME', 'mobile')

在 .env 文件中设置您的私有配置,例如


SMS_LINE_NUMBER=10001010
SMS_USER_ID=12345
SMS_PASSWORD=123456789
SMS_DEFAULT_SMS_RCPT=0901***1020 // You're phone number for test on local mode or (.env => APP_ENV=local)
SMS_MOBILE_FIELD_NAME=mobile // Name of the mobile field in you're User model or users migration or users table

如何使用 va/smart-sms 包?

默认门面用于发送消息及其方法


// The default message:

$message = 'You're message;

// If we want to send a message to $user:

$user->notify(SmsNotificationFacade::message($message)); // mobile = $user->mobile, message = $message
or
$request->user->notify(SmsNotificationFacade::message($message)); // mobile = $user->mobile, message = $message

// If we want to send a mesage to another mobile number:

$user->notify(SmsNotificationFacade::message($message)->mobile('0901***1020')); // mobile = 0901***1020, message = $message

// If we want to send a message to another mobile numbers (Send Group message):

$user->notify(SmsNotificationFacade::message($message)->group(['0901***1020', '0901***3040']));

创建自定义短信通知并使用它

  • 创建一个新的通知

    php artisan make:sms-notification SendActivationNotification
    
  • 这个类是在哪个目录中创建的?

    app/Notifications/Message/SendActivationNotification.php
    
  • 此通知的内容

    <?php
    
    namespace App\Notifications\Message;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Notifications\Notification;
    use Va\SmartSms\Channels\SmartSmsChannel;
    
    class SendActivationNotification extends Notification
    {
        use Queueable;
    
        public $message;
        public $mobile;
        public $group;
    
        /**
         * SendActivationNotification constructor.
         * @param string|null $message
         * @param string|null $mobile
         * @param array $group
         */
        public function __construct(string $message = null, string $mobile = null, array $group = [])
        {
            $this->message = $message;
            $this->mobile = $mobile;
            $this->group = $group;
        }
    
        /**
         * Get the notification's delivery channels.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function via($notifiable)
        {
            return [SmartSmsChannel::class];
        }
    
        public function toChannel()
        {
            return [
                'text' => "You're message" OR $this->message, // You're default (text of message)
                'mobile' => $this->mobile,
                'group' => $this->group
            ];
        }
    }
    
    
  • 使用此通知发送消息

    // If we want to send a message with this notification
    
    $user->notify(new SendActivationNotification()); // mobile = $user->mobile, message = (Default meassge in notification)
    
    // If we want to send a message to another mobile number:
    
    $user->notify(new SendActivationNotification(null, '0901***1020')); // mobile = $user->mobile, message = (Default meassge in notification)
    
    // If we want to send a message to another mobile numbers (Send Group message):
    
    $user->notify(new SendActivationNotification(null, null, ['0901***1020', '0901***3040'])); // mobile = array, message = (Default meassge in notification)
    
    // If we want to push a message to notification class
    
    $message = "You're message";
    $user->notify(new SendActivationNotification($message)); // mobile = $user->mobile, message = $message