Laravel 8推动器事件名称不起作用' app \ Events \ EventNotify'而不是eventnotify

发布于 2025-02-04 02:36:05 字数 2056 浏览 2 评论 0原文

我正在尝试使用Pusher向Laravel API发送实时通知到Flutter应用程序。当我直接从Pusher调试控制台发送事件时,Flutter应用程序可以正确接收事件。当我从Laravel发送活动时,在Flutter应用程序方面什么都没发生,但是我可以看到该事件显示在Pusher Debug Console上。频道名称为“ ChannelNotify”,事件名称为“ EventNotify”,但是Pusher Debug Console上的接收事件显示:Channel:ChannelNotify,Event:App \ Events \ Events \ Events notify pusher的屏幕截图

而不是将事件名称称为'eventnotify',而是获得了整个路径' app \ events \ eventnotify'作为事件名称,我认为这是为什么颤动不会拦截事件的原因。我试图将事件名称更改为“ app \ events \ eventnotify”,但仍无法正常工作。 有什么方法可以强迫Laravel发送事件名称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');
    }
}

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');
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随心而道 2025-02-11 02:36:05

尝试覆盖以下方法,并使用这样的事件名称:

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

现在您应该看到正确的事件名称,而不是类名称。至少,这应该解决错误的事件名称的第一个问题。让我们看看这是否解决了问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文