PHP code example of yzh52521 / think-notification

1. Go to this page and download the library: Download yzh52521/think-notification 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/ */

    

yzh52521 / think-notification example snippets


php think make:notification InvoicePaid



namespace app\model;

use yzh52521\notification\Notifiable;

class User 
{
    use Notifiable;
}

use app\notification\InvoicePaid;

$user->notify(new InvoicePaid($invoice));


use yzh52521\facade\Notification;

Notification::send($users, new InvoicePaid($invoice));

Notification::sendNow($developers, new DeploymentCompleted($deployment));


/**
 * 获取通知发送频道
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function channels($notifiable)
{
    return $notifiable->prefers_sms ? ['sendcloud'] : ['mail', 'database'];
}



namespace app\notifications;

use think\queue\ShouldQueue;
use yzh52521\Notification;
use yzh52521\notification\message\Mail;
use yzh52521\notification\Notifiable;

class InvoicePaid extends Notification implements ShouldQueue
{

    // ...
}

$user->notify(new InvoicePaid($invoice));

 public function __construct(Order $order)
    {
         $this->order = $order;
         $this->delay =5;
    }

 public function __construct(Order $order)
    {
         $this->order = $order;
         public $connection = 'redis';
         $this->delay =5;
    }

/**
 * 获取通知的邮件描述。
 *
 * @param  mixed  $notifiable
 * @return \yzh52521\notification\messages\Mail
 */
public function toMail($notifiable)
{
    $url = url('/invoice/'.$this->invoice->id);
    return (new Mail)
                ->greeting('Hello!')
                ->line('你的一张发票已经付款了!')
                ->action('查看发票', $url)
                ->line('感谢您使用我们的应用程序!');
}


public function toMail($notifiable)
{
    return (new Mail)
        ->to($to)
        ->subject('找回密码')
        ->view('emails.name', ['invoice' => $this->invoice]);
}


public function toMail($notifiable)
{
    return (new Mail)
                ->from('[email protected]', 'Barrett Blair')
                ->line('...');
}

php think notification:table

php think migrate:run

public function toDatabase($notifiable)
{
    return [
        'invoice_id' => $this->invoice->id,
        'amount' => $this->invoice->amount,
    ];
}

$user = app\model\User::find(1);

foreach ($user->notifications as $notification) {
    echo $notification->notification_type;
}

$user = app\model\User::find(1);

foreach ($user->unreadNotifications as $notification) {
    echo $notification->notification_type;
}


$user = app\model\User::find(1);

foreach ($user->unreadNotifications as $notification) {
    $notification->markAsRead();
}

$user->unreadNotifications->markAsRead();


$user = app\model\User::find(1);
$user->unreadNotifications()->update(['read_time' => now()]);

$user->notifications()->delete();

public function toSendcloud($notifiable)
{
    return (new Sendcloud($user,$key))
                ->data('Your SMS message content');
}


public function toSendcloud($notifiable)
{
    return (new Sendcloud($user,$key))
                ->data('Your SMS message content');
}

public function toSendcloud($notifiable)
{
    return (new Sendcloud($user,$key))
                ->to('15556666666')
                ->data('Your SMS message content');
}

public function toSendcloud($notifiable)
{
    return (new Sendcloud($user,$key))
                ->to('15556666666')
                ->template('40438')
                ->data('Your SMS message content');
}


public function toSendcloud($notifiable)
{
    return (new Sendcloud($user,$key))
                ->to('15556666666')
                ->isMultimedia()
                ->data('Your SMS message content');
}



    
public function toSendcloud($notifiable)
{
    return (new Sendcloud($user,$key))
                ->to('15556666666')
                ->isVoice()
                ->data('Your SMS message content');
}

$config = [
    // HTTP 请求的超时时间(秒)
    'timeout' => 5.0,

    // 默认发送配置
    'default' => [
        // 网关调用策略,默认:顺序调用
        'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

        // 默认可用的发送网关
        'gateways' => [
            'aliyun',
        ],
    ],
    // 可用的网关配置
    'gateways' => [
        'errorlog' => [
            'file' => '/tmp/easy-sms.log',
        ],
        'aliyun' => [
            'access_key_id' => '',
            'access_key_secret' => '',
            'sign_name' => '',
        ],
        //...
    ],
];


namespace app\notification;

use yzh52521\notification\message\Easysms;

class ValidateCode extends Notification
{

    public function channels($notifiable)
    {
         return ['easysms'];
    }
    
    public function toEasysms($notifiable)
    {
       return ( new Easysms )
               ->to('13188888888') 
               ->template('SMS_001')  
               ->content('您的验证码{$code},该验证码5分钟内有效,请勿泄漏于他人!')
               ->data(['code' =>6379]);
    }
}


namespace app\notification;

use yzh52521\notification\Notifiable;
use yzh52521\notification\Channel;
use yzh52521\Notification;

class VoiceChannel extends Channel
{

     /**
     * 发送给定的通知
     * @param Notifiable $notifiable
     * @param Notification $notification
     * @return mixed
     */
    public function send($notifiable, Notification $notification): void
    {
        $message = $notification->toVoice($notifiable);

        // 将通知发送给 $notifiable 实例...
    }
}

use yzh52521\Notification;
use yzh52521\notification\Notifiable;

class InvoicePaid extends Notification
{

    /**
     * 获取通知频道
     * @param Notifiable $notifiable
     */
    public function channels( $notifiable)
    {
        return VoiceChannel::class;
    }

    /**
     * 获取通知的语音表示形式
     * @param Notifiable $notifiable
     */
    public function toVoice($notifiable): VoiceMessage
    {
        // ...
    }
}


class VoiceMessage 
{
     // ...
}