dstpabuaran.com/app/Notifications/WebPushNotification.php

41 lines
1013 B
PHP

<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use NotificationChannels\WebPush\WebPushChannel;
use NotificationChannels\WebPush\WebPushMessage;
class WebPushNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(
public string $title,
public string $body,
public string $icon = '/icon-192x192.png',
public ?string $url = null,
) {}
public function via(object $notifiable): array
{
return [WebPushChannel::class];
}
public function toWebPush(object $notifiable, mixed $notification): WebPushMessage
{
$webPushMessage = (new WebPushMessage)
->title($this->title)
->icon($this->icon)
->body($this->body);
if ($this->url) {
$webPushMessage->data(['url' => $this->url]);
}
return $webPushMessage;
}
}