phplaravellaravel-notificationlaravel-jobs

how to send notification to user after dispatching batch job


I'm trying to send bulk sms with batch jobs, I want to send more than 1000 sms through the result of a user filter. I do a one time job to do a foreach to save a series of jobs and then submit another job with the bus. After sending all the sms, I want to send a notification to the user saying that the messages have already been sent, but every time I try to do this my PC's memory goes to infinity and my PC freezes, if I send without notification, the jobs works normally. I also created a test controller to separately send a notification to check if it works and it also worked. Is there any solution???

my job

foreach($this->result as $key => $filter) {
            $jobs[] = new ProcessSendSms($filter, $this->message);
        }

        $notificationService = new NotificationService();
       
        Bus::batch($jobs)
            ->finally(function (Batch $batch) use ($notificationService) {
                $notificationService->notificationBulkSms(
                    'test@email.com.br', 
                    $this->message
                );
            
            })
            ->allowFailures()
            ->dispatch();

my service

 public static function notificationBulkSms($email, $message)
    {
        Notification::route('mail', [
            $email => 'Test'
        ])->notify(new BulkSmsFinished($message));

        return true;
    }

my laravel notification class

 public function __construct($message)
    {
        $this->message = $message;
    }

public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('sms finished')
                    ->line('Message: ' . $this->message)
                    ->subject('sms finished')
    }

Solution

  • I found the problem, it was a scope problem, my "this->message" comes from the construct job class and the function finally gets the "this" from its own scope, so it was just a matter of declaring the variable before the batch function and incrementing it at closure.