File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Jobs/Notifications/SendNotificationJob.php
<?php
namespace App\Jobs\Notifications;
use App\Http\Controllers\PushNotificationController;
use App\Jobs\Notifications\Interfaces\SendNotification;
use App\Notification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class SendNotificationJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public SendNotification $sendNotification;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(SendNotification $sendNotification)
{
$this->sendNotification = $sendNotification;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$push = new PushNotificationController();
// Enviar la notificación
$onesignalNotificationID = $push->sendToUser(
$this->sendNotification->getMessage(),
$this->sendNotification->getOnesignalId(),
null,
$this->sendNotification->getData(),
null,
$this->sendNotification->getSchedule(),
$this->sendNotification->getTypeTarget()
);
// Registrar la notificación en la base de datos
if ($onesignalNotificationID && Notification::where('id', $this->sendNotification->getNotificationId())->exists()) {
$notification = Notification::find($this->sendNotification->getNotificationId());
$notification->reference_onesignal_notification_id = $onesignalNotificationID;
$notification->update();
}
Log::info('Notificacion de usuario enviada', [
'notification_id' => $this->sendNotification->getNotificationId(),
'user_id' => $this->sendNotification->getOnesignalId(),
'onesignal_id' => $onesignalNotificationID,
'message' => $this->sendNotification->getMessage(),
'data' => $this->sendNotification->getData(),
'schedule' => $this->sendNotification->getSchedule(),
'type_target' => $this->sendNotification->getTypeTarget(),
]);
}
}