How can i send notification to all users devices using Laravel Job?

441 Views Asked by At

I want to send a notification to all users when I click on send notification button it shows a success message on the spot without taking time and it sends a notification to all users in the background in the queue using the larval job. But when I click on the button it shows me a success message and sends form data into the job table it cannot remove the queue data from the table nor send a notification to users. I cahnge my .env file QUEUE_CONNECTION=databse. And also use the Php artisan queue: work command but it does nothing to work

Controller code

   public function notification(Request $request)
    {
        $title = $request->input('title');
        $body = $request->input('body');
        $role = $request->input('role');

        $notificationData = [
            'title' => $title,
            'body' => $body,
            'role' => $role
        ];

        $notification = Notification::create($notificationData); // Create a new instance of Notification

        SendNotificationJob::dispatch($notification)->onQueue('notifications')->delay((now()->addMinutes(1))); // Pass the notification instance

        $request->session()->flash('success', 'Notification sent successfully!');

        return redirect()->back();
    }

And My SendNotificationJob.php code is here

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Notification;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class SendNotificationJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $notification;

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

    public function handle()
    {
        $notification = [
            'title' => $this->notification->title,
            'body' => $this->notification->body,
        ];

        $notification = Notification::create($notification);
        $users = User::where('role', $this->notification->role)->get();

        $deviceIds = $users->pluck('device_id')->toArray();
        Log::debug($users);
        $url = 'my FCM url';
        $serverKey = 'my server key ';
        $headers = [
            'Content-Type' => 'application/json',
            'Authorization' => 'key=' . $serverKey,
        ];
        $notificationData = [
            'title' => $this->notification->title,
            'body' => $this->notification->body,
        ];

        foreach ($deviceIds as $deviceId) {
            $notifBody = [
                'notification' => $notificationData,
                'to' => $deviceId,
            ];

            $response = Http::withHeaders($headers)->post($url, $notifBody);

            if ($response->successful()) {
                Log::info('Notification sent successfully to device: ' . $deviceId);
            } else {
                Log::error('Failed to send notification to device: ' . $deviceId);
            }
        }
    }
}

I want After Php artisan queue: work it sends a notification to all users in the background and empties the job table.

0

There are 0 best solutions below