プッシュ通知
プッシュ通知は Web Push Protocol と VAPID (Voluntary Application Server Identification) を使用して、ブラウザにネイティブ通知を送信します。
アーキテクチャ
Section titled “アーキテクチャ”サブスクリプション登録
Section titled “サブスクリプション登録”VAPID 公開鍵の取得
Section titled “VAPID 公開鍵の取得”Bell RPC: getVapidKey()→ { publicKey: string }ブラウザでのサブスクリプション
Section titled “ブラウザでのサブスクリプション”// Service Worker 登録const registration = await navigator.serviceWorker.ready;
// Push サブスクリプション作成const subscription = await registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: urlBase64ToUint8Array(publicKey)});
// サーバーに登録const { endpoint, keys } = subscription.toJSON();await registerPushDevice({ shopId, endpoint, p256dh: keys.p256dh, auth: keys.auth, deviceName: navigator.userAgent, registeredBy: userId, registeredByName: userName});サブスクリプションデータ
Section titled “サブスクリプションデータ”| フィールド | 説明 |
|---|---|
endpoint | Push Service のエンドポイント URL |
p256dh | クライアント公開鍵(P-256 DH) |
auth | 認証シークレット |
deviceName | デバイス名(UserAgent) |
registeredBy | 登録者のユーザー ID |
HTTP レスポンスの処理
Section titled “HTTP レスポンスの処理”| ステータス | 処理 |
|---|---|
| 201 | 送信成功 |
| 410 Gone | サブスクリプション削除(ブラウザで解除済み) |
| 429 | リトライ対象(レート制限) |
| 5xx | リトライ対象(サーバーエラー) |
スケジュール送信
Section titled “スケジュール送信”PUSH メッセージは予約送信が可能です。
Bell RPC: sendPushMessage({ targetUserId, targetType, title, body, url?, priority?, scheduledAt, createdBy})スケジューリングロジック
Section titled “スケジューリングロジック”| 条件 | 動作 |
|---|---|
scheduledAt が8時間以内 | 即座にキューへ投入(delaySeconds 付き) |
scheduledAt が8時間以降 | DB に保存、Cron でキューイング |
Cron トリガー(毎時0分)が scheduledAt <= NOW + 8h の未送信メッセージを検出してキューに投入します。
デバイス管理
Section titled “デバイス管理”店舗 PUSH デバイス
Section titled “店舗 PUSH デバイス”店舗ごとに複数の PUSH デバイスを登録できます。
Bell RPC: registerShopPushDevice({ shopId, endpoint, p256dh, auth, ... })Bell RPC: removeShopPushDevice({ id })Bell RPC: updateShopPushCategories({ id, categories })汎用 PUSH サブスクリプション
Section titled “汎用 PUSH サブスクリプション”ユーザー単位の PUSH サブスクリプション管理です。
Bell RPC: subscribe({ userId, userType, endpoint, p256dh, auth, ... })Bell RPC: unsubscribe({ endpoint })Bell RPC: listSubscriptions({ userId })Bell RPC: updateSubscriptionSettings({ id, settings })Bell RPC: deleteSubscription({ id })各デバイスにテスト通知を送信できます。
Bell RPC: sendShopTestNotification({ shopId, channel: 'push', targetId: subscriptionId})- 通知システム 概要 - 全体アーキテクチャ
- 店舗通知設定 - デバイス・カテゴリ管理
- Bell RPC メソッド - PUSH 関連 RPC
- Bell キュー・Cron 処理 - push-notifications キュー