如何使用 Livewire 通知其他用户(例如 Facebook 铃声图标)?

问题描述 投票:0回答:1

我有这个方法

\Livewire\LeadManager.php

public function compartilharLead($brokerId, $leadId)
{
    $lead = Lead::find($leadId);
    if ($lead) {
        $lead->user_id = $brokerId;
        $lead->class = 'border-warning';
        $lead->save();

        User::find($brokerId)->notify(new LeadRecebido($lead));
        $this->emit('LeadCompartilhado');
        $this->emit('hideModal');
        $this->mount();
    } else {
        $this->emit('exibirToastErro', 'ERRO', 'Lead não encontrado!');
    }
}

这对我的用户来说工作正常,但在另一个用户登录的另一个浏览器窗口上没有任何反应。 打开两个浏览器窗口,在正常导航和一个私人导航上,我和另一位用户登录。 当我向我分享线索时,它工作正常,铃铛图标会显示一条新消息,就像在 Facebook 上一样,但当我分享给其他用户时,除非我重新加载页面,否则什么也不会发生。

让我们看更多代码:

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Illuminate\Support\Facades\Auth;

class Notifications extends Component
{
    public $novosLeads;
    protected $listeners = ['LeadCompartilhado' => 'atualizaLeads'];


public function mount()
{
    $this->pegaLeads();
}

public function atualizaLeads()
{
    $this->pegaLeads();
    $this->render();
}

public function pegaLeads()
{
    $this->novosLeads = Auth::user()->unreadNotifications;
}

public function marcarComoLido($leadId)
{
    $lead = Auth::user()->notifications()->where('id', $leadId)->first();
    if ($lead) {
        $lead->markAsRead();
        $this->pegaLeads(); // Atualizar a lista após marcar como lido
    }
}

public function render()
{
    return view('livewire.notifications');
}
}

我必须使用某种推送服务吗?仅使用 Livewire 无法做到这一点吗?

notifications laravel-livewire
1个回答
0
投票

这个很棒的教程救了我的命。谢谢亲爱的@ceejayoz

雷普罗杜齐尔·图多 Laravel-Livewire 聊天消息应用程序(实时)从头开始

© www.soinside.com 2019 - 2024. All rights reserved.