【问题标题】:get variable when createing custom notification channel in laravel在 laravel 中创建自定义通知通道时获取变量
【发布时间】:2021-05-13 11:13:51
【问题描述】:

在 Laravel 文档中,有部分解释了如何制作自定义通知类,网址是:

https://laravel.com/docs/5.4/notifications#custom-channels

所以我创建了一个名为 `MessageReplied' 的通知类,我想为它定义一个自定义的 SMS 通道,

MessageReplied 类中的代码如下:

<?php

namespace App\Notifications;

use App\Channels\SmsChannel;
use App\WorkCase;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class MessageReplied extends Notification
{
use Queueable;
public $workCase;

/**
 * Create a new notification instance.
 *
 * @param WorkCase $workCase
 */
public function __construct(WorkCase $workCase)
{
    $this->workCase = $workCase;
}

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['database', 'mail', SmsChannel::class];
}

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
                ->markdown('mail.message_replied', ['workCase' => $this->workCase])
                ->subject('new Message');
}

/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toArray($notifiable)
{
    return [
        'workCase' => $this->workCase
    ];
}

/**
 * @return array
 */
public function toSms()
{
    return [
        'foo' => 'bar'
    ];
}


}

我的自定义频道称为 SmsChannel 并包含:

<?php

namespace App\Channels;
use App\WorkCase;
use GuzzleHttp\Client;
use Illuminate\Notifications\Notification;
use function MongoDB\BSON\toJSON;

class SmsChannel
{

/**
 * Send the given notification.
 *
 * @param  mixed  $notifiable
 * @param  \Illuminate\Notifications\Notification  $notification
 * @return void
 */
public function send($notifiable, Notification $notification)
{
    var_dump($notifiable);
    $text = !!!How Can I Get this variable from MessageReplied Class ???;

    $guzzle = new Client();
     $guzzle->post('https://api.exapme.com/v1/93253374C30696465434E325645513D3D/sms/send.json', [
        'form_params' => [
            'receptor' => $notification->workCase->client->cellphone,
            'message' => $text,
//              'sender'  => config('sms.sender')
        ],
        'verify' => false,
    ]);
}

}

正如您在 SmsChannel 类中看到的,我如何获得在 MessageReplied 类中设置的 bar 值?

【问题讨论】:

    标签: laravel laravel-5 notifications sms


    【解决方案1】:

    来自documentation

    $message = $notification->toSms();

    【讨论】:

      【解决方案2】:

      在 SmsChannel 类的发送方法中:

      public function send($notifiable, Notification $notification)
      {
          $message = $notification->toSms(notifiable);
          $bar = $message['bar'];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-17
        • 2017-04-23
        • 1970-01-01
        • 2021-05-06
        • 1970-01-01
        • 2014-02-09
        • 1970-01-01
        • 2019-01-13
        相关资源
        最近更新 更多