我正在用Laravel構建一個API,並且想要使用Laravel Notifications系統發送推送通知。我有一個匹配模型(基本上是一個帖子),另一個用戶可以喜歡這個匹配。當喜歡這場比賽時,該帖子的創建者將得到推送通知。它就像Instagram,Facebook等。沒有關於型號[App Models Match]的查詢結果
通常推送通知不發送給用戶。我安裝了Laravel Horizon以查看是否存在錯誤。有時通知是發送的,有時不是。具有完全相同的數據:
通知有時具有完全相同的數據(同一用戶,相同的匹配)失敗。
該錯誤是如下:
照亮\數據庫\鋒\ ModelNotFoundException:否查詢模型導致 [應用\模型\匹配]在 /home/forge/owowgolf.com/vendor 118 /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;
}
}
顯示通知文件的代碼。 – Vikash
添加了'NewLikeOnPost'通知類的代碼。 –
我不確定,但是您嘗試訪問作業中的會話數據的任何地方,或者您在隊列中處理的任何函數或對象正在使用會話? – Vikash