spacedog4
1 month ago
109
1
Laravel

Prevent broadcast notification to be added to the queue

Posted 1 month ago by spacedog4

I'm using queue to generate some document, after generating it, I send a notification to the user, telling that his documents has been generated, but after sending the notification, laravel add the broadcast to the end of the queue, so at the end of the day, the user will be notified just when all files has been generated

How can I prevent my notification to be added to the queue?

This is my notification class:

<?php

namespace App\Notifications;

use App\Report;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Log;

class MovementReportStatusChanged extends Notification {

    /**
     * @var Report
     */
    private $report;

    /**
     * Create a new notification instance.
     *
     * @param Report $report
     */
    public function __construct(Report $report)
    {
        //
        $this->report = $report;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        if ($notifiable->settings->disabled_notifications && in_array(class_basename($this), $notifiable->settings->disabled_notifications)) {
            return [];
        }

        return ['database', 'broadcast'];
    }

    public function toArray($notifiable)
    {
        if ($this->report->status == 'finished') {
            return [
                'title' => 'Relatório concluído',
                'body'  => "Seu relatório já está pronto, clique aqui para acessa-lo",
                'link'  => $this->report->file,
                'blank' => true,
                'icon'  => 'movement'
            ];
        } else if ($this->report->status == 'failed') {
            return [
                'title' => 'Relatório falhou',
                'body'  => "Desculpe, algo deu errado ao gerar o relatório, tente novamente",
                'link'  => null,
                'icon'  => 'movement'
            ];
        }

        return [];
    }
}

I tryied adding these lines as I thought it would work after reading this discussion on github, but it didn't

https://github.com/laravel/framework/pull/16867

public function broadcastNow()
    {
        return true;
    }

Please sign in or create an account to participate in this conversation.