"use client"; import { useState } from "react"; type PushTestPanelProps = { initialSubscriptionCount: number; deviceCount: number; }; export function PushTestPanel({ initialSubscriptionCount, deviceCount, }: PushTestPanelProps) { const [submitting, setSubmitting] = useState(false); const [subscriptionCount, setSubscriptionCount] = useState( initialSubscriptionCount, ); const [notice, setNotice] = useState(null); async function handleTestPush() { setSubmitting(true); setNotice(null); try { const response = await fetch("/api/push/test", { method: "POST", }); const payload = (await response.json().catch(() => null)) as | { error?: string; targetedCount?: number; sentCount?: number; removedCount?: number; failedCount?: number; } | null; if (!response.ok) { throw new Error(payload?.error || "测试推送发送失败。",); } const targetedCount = payload?.targetedCount || 0; const removedCount = payload?.removedCount || 0; const sentCount = payload?.sentCount || 0; const failedCount = payload?.failedCount || 0; setSubscriptionCount(Math.max(0, targetedCount - removedCount)); if (targetedCount === 0) { setNotice("当前账号还没有任何开启消息提醒的设备,先在要接收通知的设备上打开消息提醒。\n"); return; } const fragments = [`已向 ${targetedCount} 台设备发出测试通知。`]; if (sentCount > 0) { fragments.push(`成功送出 ${sentCount} 条。`); } if (removedCount > 0) { fragments.push(`清理了 ${removedCount} 个失效订阅。`); } if (failedCount > 0) { fragments.push(`${failedCount} 台设备本次发送失败。`); } setNotice(`${fragments.join(" ")}\n`); } catch (error) { setNotice( error instanceof Error ? error.message : "测试推送发送失败。", ); } finally { setSubmitting(false); } } return (

推送测试

给当前账号的所有设备发一条测试通知

用来确认这位家属账号下已经开启提醒的设备,是否都能正常收到推送。

已连接设备 {deviceCount} 已开启提醒的设备 {subscriptionCount}
{notice ? (

{notice}

) : null}
); }