0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel×Webプッシュ通知

Last updated at Posted at 2025-10-02

参考にさせていただいた素晴らしいサイト様
基本的にはこちらをそのまま実行すれば実装できます。

バージョンの問題か、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)
        );
    }
});
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?