SendManualPushJob.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Jobs;
  3. use App\Enums\PushNotificationTargetEnum;
  4. use App\Models\User;
  5. use App\Notifications\Push\Manual\ManualPush;
  6. use App\Services\PushNotificationService;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Facades\Log;
  13. class SendManualPushJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. public int $tries = 2;
  17. public int $timeout = 120;
  18. public array $backoff = [15, 60];
  19. /**
  20. * @param int[] $userIds
  21. */
  22. public function __construct(
  23. public readonly array $userIds,
  24. public readonly string $title,
  25. public readonly string $body,
  26. public readonly PushNotificationTargetEnum $target,
  27. ) {}
  28. public function handle(): void
  29. {
  30. try {
  31. $pushService = app(PushNotificationService::class);
  32. } catch (\Throwable $exception) {
  33. Log::error('Falha ao resolver PushNotificationService na push manual', [
  34. 'error' => $exception->getMessage(),
  35. ]);
  36. return;
  37. }
  38. $notification = new ManualPush($this->title, $this->body, $this->target);
  39. foreach (User::whereIn('id', $this->userIds)->get() as $user) {
  40. try {
  41. $pushService->sendToUser($user, $notification);
  42. } catch (\Throwable $exception) {
  43. Log::error('Falha ao enviar push manual', [
  44. 'user_id' => $user->id,
  45. 'target' => $this->target->value,
  46. 'error' => $exception->getMessage(),
  47. ]);
  48. }
  49. }
  50. }
  51. }