M
M
Maxim2020-01-16 12:13:47
Push technologies
Maxim, 2020-01-16 12:13:47

Laravel Pusher not showing notifications?

Why does not display a notification for user 12, opened the browser and logged in under it, but there is nothing
in the log

Pusher : State changed : connecting -> connected with new socket ID 127854.10730967
pusher.min.js:8 Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"my-channel"}}
pusher.min.js:8 Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","channel":"my-channel","data":{}}
pusher.min.js:8 Pusher : No callbacks on my-channel for pusher:subscription_succeeded

call an event
event(new UserNotification(User::where('id','=', 12)->first(), 'Вас лайкнули'));

the event itself
namespace App\Events;

use App\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class UserNotification  implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */

    /**
     * @var User
     */
    public $user;

    /**
     * @var array
     */
    public $data;

    /**
     * Create a new event instance.
     * @param array $data
     */

    public function __construct(User $user, $data)
    {
        $this->user = $user;
        $this->data = $data;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return ['my-channel'];
    }
 public function broadcastAs()
    {
        return 'my-event';
    }
}

and pasted the code
<script src="https://js.pusher.com/5.0/pusher.min.js"></script>
  <script>

    // Enable pusher logging - don't include this in production
    Pusher.logToConsole = true;

    var pusher = new Pusher('122121212', {
      cluster: 'eu',
      forceTLS: true
    });

    var channel = pusher.subscribe('my-channel');
    channel.bind('my-event', function(data) {
      alert(JSON.stringify(data));
    });
  </script>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question