在 Laravel 中创建自定义通知通道时获取变量



在Laravel文档中,有一节解释了如何创建自定义通知类,URL是:

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

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

在 MessageReply 类中,代码如下所示:

<?php
namespace AppNotifications;
use AppChannelsSmsChannel;
use AppWorkCase;
use IlluminateBusQueueable;
use IlluminateNotificationsNotification;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
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 IlluminateNotificationsMessagesMailMessage
*/
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'
];
}

}

我的自定义频道称为短信频道,包含:

<?php
namespace AppChannels;
use AppWorkCase;
use GuzzleHttpClient;
use IlluminateNotificationsNotification;
use function MongoDBBSONtoJSON;
class SmsChannel
{
/**
* Send the given notification.
*
* @param  mixed  $notifiable
* @param  IlluminateNotificationsNotification  $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,
]);
}
}

正如您在短信频道类中看到的bar如何获取在消息回复类中设置的值?

来自文档:

$message = $notification->toSms((;

在SmsChannel 类上的 send 方法中:

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

最新更新