参考にさせていただいた素晴らしいサイト様
基本的にはこちらをそのまま実行すれば実装できます。
バージョンの問題か、Modelのところでエラーが出てしまうので、WebPushのPushSubscriptionを継承したクラスへと変更してみました。
AnonymousPushNotification.php
<?php
namespace App\Models;
use NotificationChannels\WebPush\PushSubscription;
class AnonymousPushSubscription extends PushSubscription
{
protected $table = 'anonymous_push_subscriptions';
protected $guarded = ['id'];
// もし key で匿名ユーザーを識別するなら
public function scopeForKey($query, $key)
{
return $query->where('key', $key);
}
}
通知をクリックしたときにリンクを開く
anonymous_push_sw.js
// 🔹 通知クリック時の処理
self.addEventListener('notificationclick', event => {
event.notification.close(); // 通知を閉じる
// クリックされた通知に URL データがある場合は開く
if (event.notification.data && event.notification.data.url) {
event.waitUntil(
clients.openWindow(event.notification.data.url)
);
}
});