phplaravelflutterpush-notificationpusher

Laravel 8 pusher event name not working 'App\Events\eventnotify' instead of eventnotify


I'm trying to send real time notification from Laravel api to flutter app using pusher. When I send an event directly from pusher debug console, the flutter app receive correctly the event. When I send the event from Laravel nothing happens in the flutter app side, but I can see that the event shows on pusher debug console. the channel name is 'channelnotify' and the event name is 'eventnotify' but the received event on pusher debug console shows : Channel: channelnotify, Event: App\Events\eventnotify screenshot from pusher

instead of getting the event name as 'eventnotify', pusher gets the whole path 'App\Events\eventnotify' as event name, and I think this why the flutter doesn't intercept the event. I tried to change the event name to 'App\Events\eventnotify' in my flutter app but still not working. is there any way to force laravel to send the event name whithout path?

//send event api: 
 public function sendNotif($to_id,$context ,$context_id ,$label){

        $data = array(
            'to_id' =>$to_id,
            'context' =>$context,
            'context_id' =>$context_id,
            'label' =>$label,
            );
    
            $result = [];
            array_push($result,$data);

            $str_json = json_encode($result);
           
            event(new eventnotify($str_json));

            return $str_json;

    }

//event code
<?php

namespace App\Events;

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

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

    public $notif;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('channelnotify');
    }
}

Solution

  • Try overriding the following method and using the name of your event like this:

    /**
     * The event's broadcast name.
     *
     * @return string
     */
    public function broadcastAs()
    {
        return 'eventnotify';
    }
    

    Now you should see the correct event name, instead of the class name. At least, this should solve the first problem of the wrong event name. Let's see if this solves the issue.