mercredi 28 décembre 2016

Laravel 5.3 Pusher notifications not received by Echo

I'm trying to broadcast a simply test notification to a Laravel app using Pusher.

Within my web page, I've got this JavaScript to listen for the broadcast:

import Echo from 'laravel-echo'

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

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

My BroadcastServiceProvider is configured to validate the private channel request:

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

When I check the Pusher console, I can see that the code above successfully subscribed:

enter image description here

When I run this test, I expect it to send a notification but nothing actually shows up on the console.log or in the toastr notification browser-side:

enter image description here

The SendMessage is just a simple test notification:

namespace App\Notifications;

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

/**
 * Send a simple generic notification message.
 *
 * @package App\Notifications
 */
class SendMessage extends Notification
{
    use Queueable;

    /**
     * @var string
     */
    public $message = '';

    /**
     * @var string
     */
    public $subject = '';

    /**
     * @var array
     */
    private $via = ['broadcast'];

    /**
     * Create a new notification instance.
     *
     * @param string $message
     * @param string $subject
     */
    public function __construct($message, $subject = '')
    {
        $this->message = $message;
        $this->subject = $subject;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return $this->via;
    }

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

I also tried triggering it from the Laravel side, but it still doesn't reach the user's browser:

$user = User::find($this->argument('user'));

$user->notify(new SendMessage('Testing', 'Hey'));

What am I missing here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire