php - Receive Notificatoin Pusher + Echo in Laravel

602

I am trying to receive notification using echo + pusher. I can post to pusher and pusher console receive the event and channel but i can't get this data on laravel. I read many documents, but none of them work for me.

WebNotification Class // Notification file

private $subscription;
public function __construct($data)
{
    $this->subscription = $data;
}
 public function via($notifiable)
 {
     return ['database','broadcast'];
 }

public function toBroadcast($notifiable)
{
    return new BroadcastMessage([
        'data' => $this->subscription,
        'count' => $notifiable->unreadNotifications->count()
    ]);
}

Account Model

 <?php

 namespace App;

 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Notifications\Notifiable;

 class account extends Model
 {
  use Notifiable;

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

MyNotification // To call event to send data to pusher

   public function DatabaseNot(){
      $user = Account::where('id', Cookie::get('uid'))->first();
      $data = collect(['title'=>'Hello Title', 'body'=>'my body']);
      $user->notify(new WebNotification($data));

      return view('layout.test',['user'=>$user]);
   } 

Pusher log

     Channel: private-accounts.23, Event: Illuminate\Notifications\Events\BroadcastNotificationCreated

I call Echo in resorces/js/app.js

  Echo.private('App.accounts.23')
   .notification((notification) => {
        console.log(notification.type);
     });

I did not get any response from this. I already add csrf token in meta, I compiled js code using NPM run watch. I tried many other ways, no one provide clear document, its so confusing.

People are also looking for solutions to the problem: php - Multiple DB lookups VS fetch all + array search, what's more efficient

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.