【发布时间】:2018-03-16 19:51:45
【问题描述】:
我正在使用 Laravel 构建一个 API,并希望使用 Laravel 通知系统发送推送通知。我有一个匹配模型(基本上是一个帖子),另一个用户可以喜欢这个匹配。当比赛被点赞时,帖子的创建者将收到推送通知。就像 Instagram、Facebook 等一样。
推送通知通常不会发送给用户。我安装了 Laravel Horizon 看看是否有错误。有时通知已发送,有时未发送。使用完全相同的数据:
通知有时会因为完全相同的数据(相同的用户,相同的匹配)而失败。
报错如下:
Illuminate\Database\Eloquent\ModelNotFoundException: 没有查询结果 对于模型 [App\Models\Match] 118 in /home/forge/owowgolf.com/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:312
我确定匹配且用户存在于数据库中,我已在发送通知之前验证了这一点。有人知道出了什么问题吗?我在网上能找到的一切都是人们在将通知发送到队列之前没有保存他们的模型。但是,如果模型不存在,甚至不会到达代码将通知发送到队列的那一行。由于路由/控制器中的隐式绑定。
控制器方法:
/**
* Like a match.
*
* @param \App\Models\Match $match
* @return \Illuminate\Http\JsonResponse
*/
public function show(Match $match)
{
$match->like();
$players = $match->players()->where('user_id', '!=', currentUser()->id)->get();
foreach ($players as $user) {
$user->notify(new NewLikeOnPost($match, currentUser()));
}
return ok();
}
通知:
<?php
namespace App\Notifications;
use App\Models\Match;
use App\Models\User;
use Illuminate\Bus\Queueable;
use NotificationChannels\Apn\ApnChannel;
use NotificationChannels\Apn\ApnMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
class NewLikeOnPost extends Notification implements ShouldQueue
{
use Queueable;
/**
* The match instance.
*
* @var \App\Models\Match
*/
private $match;
/**
* The user instance.
*
* @var \App\Models\User
*/
private $user;
/**
* Create a new notification instance.
*
* @param \App\Models\Match $match
* @param \App\Models\User $user
*/
public function __construct(Match $match, User $user)
{
$this->user = $user;
$this->match = $match;
$this->onQueue('high');
}
/**
* Get the notification's delivery channels.
*
* @param \App\Models\User $notifiable
* @return array
*/
public function via($notifiable)
{
if ($notifiable->wantsPushNotification($this)) {
return ['database', ApnChannel::class];
}
return ['database'];
}
/**
* Get the mail representation of the notification.
*
* @param \App\Models\User $notifiable
* @return \NotificationChannels\Apn\ApnMessage
*/
public function toApn($notifiable)
{
return ApnMessage::create()
->badge($notifiable->unreadNotifications()->count())
->sound('success')
->body($this->user->username . ' flagged your match.');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'user_id' => $this->user->id,
'body' => "<flag>Flagged</flag> your match.",
'link' => route('matches.show', $this->match),
'match_id' => $this->match->id,
];
}
/**
* Get the match attribute.
*
* @return \App\Models\Match
*/
public function getMatch()
{
return $this->match;
}
}
【问题讨论】:
-
显示通知文件的代码。
-
添加
NewLikeOnPost通知类的代码。 -
我不确定,但是您尝试访问作业中的会话数据或您在队列中处理的任何函数或对象的任何地方都在使用会话?
-
无论是什么原因,您都可能面临 postgresql 行锁(主要是过度使用 currentUser() 方法)。检查这篇文章它可能对你有帮助:stackoverflow.com/questions/1063043/…
-
这与使用相同队列系统的同一服务器上的多个环境有关。暂存的事件/作业正在生产队列中执行。
标签: php laravel laravel-5 queue laravel-horizon