【发布时间】:2017-12-12 15:17:56
【问题描述】:
我正在尝试在我正在开发的应用程序中构建实时通知系统。其中一项要求是,当 ID 过期时,应向该特定用户发送通知。由于这个任务需要每天最多运行一次,我开发了一个很容易与 CRON 作业一起运行的工匠命令,即 Laravel 调度程序。一切正常,即运行 artisan 命令并生成通知并将其存储在数据库和所有相关内容中。但是每次生成通知时,都需要重新加载页面,这就是我卡住的地方。我正试图让它实时发生,但抛出了一个非常奇怪的错误,我不知道这意味着什么。
这是必要的代码:
Artisan.file
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\User;
use Carbon\Carbon;
use App\Notifications\UserIdExpired;
class UpdateCatalog extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check:expiry';
/**
* The console command description.
*
* @var string
*/
protected $description = 'dummy command to check its purpose';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$ZERO = 0;
$MONTH = 30;
$today = Carbon::today();
$users = User::all();
foreach($users as $user){
$today = Carbon::today();
$expiryDate = $user->qidexpire_on;
if($today->diffInDays($expiryDate, false) <= $MONTH && $today->diffInDays($expiryDate, false) >= $ZERO){
$this->info($user);
$this->info($expiryDate->diffInDays($today));
$user->notify(new UserIdExpired);
} else {
}
}
}
}
}
通知文件
<?php
namespace App\Notifications;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\BroadcastMessage;
class UserIdExpired extends Notification
{
use Queueable;
public function via($notifiable)
{
return ['database', 'broadcast'];
}
public function toDatabase($notifiable)
{
return [
'user' => $notifiable,
'id_expired' => Carbon::now()
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'user' => $notifiable,
'id_expired' => Carbon::now()
]);
}
}
当我从控制台运行php artisan check:expiry 时,会生成通知并在页面重新加载时更新通知的数量,但它不会实时发生。以下是控制台上显示的错误
[Illuminate\Broadcasting\BroadcastException]
注意:每当我重新加载页面时,Pusher 控制台都会显示相应的日志,例如已连接的私人频道和主机以及所有这些东西,这意味着问题不在客户端,(还)
【问题讨论】:
标签: notifications laravel-5.4 pusher