2017-10-05 65 views
4

我正在用Laravel构建一个API,并且想要使用Laravel Notifications系统发送推送通知。我有一个匹配模型(基本上是一个帖子),另一个用户可以喜欢这个匹配。当喜欢这场比赛时,该帖子的创建者将得到推送通知。它就像Instagram,Facebook等。没有关于型号[App Models Match]的查询结果

通常推送通知不发送给用户。我安装了Laravel Horizo​​n以查看是否存在错误。有时通知是发送的,有时不是。具有完全相同的数据:

Laravel Horizon list

通知有时具有完全相同的数据(同一用户,相同的匹配)失败。

该错误是如下:

照亮\数据库\锋\ 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; 
    } 
} 
+1

显示通知文件的代码。 – Vikash

+0

添加了'NewLikeOnPost'通知类的代码。 –

+0

我不确定,但是您尝试访问作业中的会话数据的任何地方,或者您在队列中处理的任何函数或对象正在使用会话? – Vikash

回答

0

检查.ENV以确保ü真正使用Redis的

BROADCAST_DRIVER=redis 
CACHE_DRIVER=redis 
SESSION_DRIVER=redis 
SESSION_LIFETIME=120 
QUEUE_DRIVER=redis 

然后清除缓存(php artisan cache:clear , php artisan view:clear),即应该清除问题