Laravel 5.3 Echo未收到Pusher通知

时间:2017-11-12 16:59:46

标签: php laravel vue.js pusher

我试图使用简单的测试通知向Laravel应用广播    Pusher.Within我的网页,我已经有了这个JavaScript来听取   在bootstrap.js中广播

 import Echo from 'laravel-echo'

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'notmyrealpusherkey',
    encrypted: true
});

这是我的Notification.vue

Echo.private('App.User.' + this.id)
    .notification((notification) => {
        console.log(notification);
        toastr.info(notification.message, notification.subject);
    });

我的BroadcastServiceProvider配置为验证私人频道请求:

Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int)$user->id === (int)$id;
});
  

当我检查Pusher控制台时,我可以看到上面的代码成功订阅了检查图像:   https://d1ro8r1rbfn3jf.cloudfront.net/ms_76665/NvEB7BgUnmIA2IR5rO1S3gF1aSP306/Pusher%2B-%2BDebug%2Bconsole%2B2016-12-28%2B19-37-19.png?Expires=1510590716&Signature=Cgykr97sBJUqljH02DfUJztcILiFTMUsUBfsF4kXoYsuJCsHf4lG-3tyhXBmCM70rWGyeCqZ7nrkgqYuOYEYs6Q41nq-ActI9B-vt6fhh5rYgekfe-HM0dERY67OqAbaLVfskq2mUp1Zl7yBPKwgNR9Fw0uzdH8q6ia9L0Za9QBLM1u6Fq3AHoN4OodJaXr5X-ifn1ZVAC7WsYVtLcnWpr3Yx0-w2nzbS4rD2S9KtMnIpsOA8bcHHWW3qoDFm5J0hIB6GVF42-vQTUNo~4B1~L8XT41coOarsF~sTVsaWTYINX93BmpVpKUEDyoarbffEuUsf4sZO6Ee5fILK1wJOQ__&Key-Pair-Id=APKAJHEJJBIZWFB73RSA

     

当我直接从推送器发送通知时,它将在控制台上发送,但不会使用触发器发送描述中的图像链接:   https://d1ro8r1rbfn3jf.cloudfront.net/ms_76665/kxgC93iHRWnMPHIdJehBgfsfUG47Lu/Pusher%2B-%2BDebug%2Bconsole%2B2016-12-28%2B19-37-56.png?Expires=1510592071&Signature=f8wvoKGnWLROJ0cXCWqkWz3EVL6rVj6PuJ~-gXpHHgrt7FlZ9gf9YO3lezW2dplmN08419w9qWJh-Cc7cpt4B1dEEwqORjL8eRz528B6l-HKHwnn96iCIE~bfwc80L9qJx9nBbLqTqnEkAlvbugcGlIrr1zB-iMlz9TMloLpYkq-2T-a1Ww8BM3SmbETVunLqhqYxUCbgK3T1swS135tqvg36sA8FWWnBe3djHgSTUuYS6Nv8MyvkF8JS1slxtMzsJ30oEzs5zc~QhEfECfoA8On8VIHP8a4VrOjUyElcrcPHG~7TwnYP8Ajje7O7EW9SNyIV7t3LSyxBGiIdsVDtQ__&Key-Pair-Id=APKAJHEJJBIZWFB73RSA

NewFriendRequest只是一个简单的测试通知:

<?php

 namespace App\Notifications;

 use Illuminate\Bus\Queueable;
 use Illuminate\Notifications\Notification;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Notifications\Messages\MailMessage;

 class NewFriendRequest extends Notification implements ShouldQueue
 {
use Queueable;

public $user;

/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct($user)
{
    $this->user = $user;
}

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['mail','broadcast','database'];
}

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
                ->line('You received a new friend request from ' . $this->user->name)
                ->action('View Profile', route('profile', ['slug' => $this->user->slug]))
                ->line('Thank you for using our Tictac!');
}

/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toArray($notifiable)
{
    return [
        'name' => $this->user->name,
        'message' => $this->user->name . ' sent you friend request.'
     ];
}


  }

我也试过从Laravel方面触发它,但它仍然无法到达用户的浏览器:

  $resp =  Auth::user()->add_friend($id);
User::find($id)->notify(new \App\Notifications\NewFriendRequest(Auth::user()));
return $resp;

1 个答案:

答案 0 :(得分:0)

您在通知as stated by the documentation中错过了toBroadcast()方法。

class NewFriendRequest extends Notification implements ShouldQueue
{
    public function toBroadcast($notifiable)
    {
        return new BroadcastMessage([
            'message' => 'Insert your message',
            'subject' => 'Insert your subject',
        ]);
    }
}