Laravel Notification not broadcasting without any error?
Asked Answered
I

2

5

I'm trying to implement a notification system with Laravel and Vue JS. I implemented everything and it needs to be work but Notification class is not broadcasting.

.env file

BROADCAST_DRIVER=pusher
PUSHER_APP_ID=40xxxx
PUSHER_KEY=5a6axxxxxx
PUSHER_SECRET=b35xxxxxx

Already run those commands below:

npm install --save laravel-echo pusher-js
composer require pusher/pusher-php-server "~2.6"

in app.js (uncommented)

App\Providers\BroadcastServiceProvider::class,

BroadcastServiceProvider.php

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

bootstrap.js

import Echo from "laravel-echo"
 window.Pusher = require('pusher-js');
 window.Echo = new Echo({
    broadcaster: 'pusher',
    key: '5a6axxxxxx',
    cluster: 'eu',
    encrypted: true,
    authEndpoint: "/broadcasting/auth"
 });
 window.Pusher.log = function(message){
    window.console.log(message);
 }
 window.Echo.private('App.User.1')
    .notification((notification) => {
        console.log(notification.type);
 });

No errors in Laravel log. In the parallel, I am checking on Pusher's Debug Console only connect and disconnect requests on there. Why it's not pushing any kind of error and why it's not working properly?

Ivo answered 22/9, 2017 at 22:19 Comment(4)
Where's your Javascript?Wagonlit
updated the question. Sorry I forgot to add itIvo
Hey, I have the same problem. How did you fix yours?Athodyd
Same problem with me.Creatine
P
4

I had the same problem. In my case, the cause of the problem was the placement of the User model by namespace App\Models\User. The first way to solve this problem is to:

in BroadcastServiceProvider.php or routes/channels.php (if laravel 5.7+)

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

The second way to solve this problem is:

In User model add method

public function receivesBroadcastNotificationsOn() {
    return 'users.'.$this->id;
}

and in BroadcastServiceProvider.php or routes/channels.php (if laravel 5.7+)

Broadcast::channel('users.{user_id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});
Phycomycete answered 12/6, 2019 at 11:25 Comment(0)
G
3

You will also need to configure and run a queue worker. All event broadcasting is done via queued jobs so that the response time of your application is not seriously affected by events being broadcast.

Read more in docs https://laravel.com/docs/9.x/broadcasting#queue-configuration

It might be due to because all broadcasting is done via queued jobs so run queue work command

php artisan queue:work

If you found this helpful so please vote : ) happy codding

Edit: This solution works but not perfact solution either I not remember why I was write that, but the right solution is that you have to uncomment serviceProvider in config/app.php

app.php

Grandsire answered 30/3, 2022 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.