Laravel 5 发送通知时未使用自定义通知通道
运行:Laravel 5 发送通知时未使用自定义通知通道,laravel-5,notifications,laravel-5.6,Laravel 5,Notifications,Laravel 5.6,运行:PHP7.1.3/Laravel5.6.* 我已经成功地使用了Laravel自带的通知通道,但是由于某些原因,定制的通知通道没有被使用,更不用说构建了 预期: 调用$user->notify(new ContactResponse())应导致调用PushChannel的发送方法 实际: 当我调用$user->notify(new ContactResponse())时,ContactResponse的via方法(PushChannel)中指定的频道从不调用send,更不用说它的构造函数了
PHP7.1.3
/Laravel5.6.*
我已经成功地使用了Laravel自带的通知通道,但是由于某些原因,定制的通知通道没有被使用,更不用说构建了
预期:
调用$user->notify(new ContactResponse())
应导致调用PushChannel的发送方法
实际:
当我调用$user->notify(new ContactResponse())
时,ContactResponse的via
方法(PushChannel)中指定的频道从不调用send
,更不用说它的构造函数了
我所做的:
我已将日志记录语句添加到所有相关方法机构,以验证问题,并了解:
- ContactResponse的
方法被调用,但其via
未被调用toPush
- 从不调用PushChannel的构造函数和
方法send
- 通过方法将“邮件”添加到ContactResponse的
中的频道&a
方法添加到类中,确实会按预期在该频道上调用toMail
ContactResponse.php
肯定不是最漂亮的解决方案,但这在不修改Laravel源代码的情况下也能正常工作:
public function via(User $notifiable)
{
// TODO: revert to proper implementation when fixed
$channels = [PushChannel::class];
if (in_array(PushChannel::class, $channels)) {
$pushChannel = App::make(PushChannel::class);
$pushChannel->send($notifiable, $this);
}
// return [PushChannel::class];
}
<?php
namespace App\Channels;
// ...imports
class PushChannel
{
public function send(User $notifiable, Notification $notification)
{
$payload = $notification->toPush($notifiable)
// business logic
}
}
public function via(User $notifiable)
{
// TODO: revert to proper implementation when fixed
$channels = [PushChannel::class];
if (in_array(PushChannel::class, $channels)) {
$pushChannel = App::make(PushChannel::class);
$pushChannel->send($notifiable, $this);
}
// return [PushChannel::class];
}