概述Push API 与 Notifications 允许站点在 Service Worker 中接收推送并显示通知。需要用户授权与应用服务器密钥(VAPID)。用法/示例const reg = await navigator.serviceWorker.register('/sw.js') const perm = await Notification.requestPermission() if (perm === 'granted') { const sub = await reg.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: vapidKey }) } self.addEventListener('push', e => { const data = e.data ? e.data.json() : {} e.waitUntil(self.registration.showNotification(data.title || '消息', { body: data.body || '' })) }) 工程建议谨慎设计通知频率与内容,避免打扰与被系统限制;提供退订入口。校验 VAPID 与服务器发送格式,确保跨端一致;存储订阅需考虑隐私与合规。在不支持环境使用应用内消息或轮询作为降级方案。参考与验证MDN:Push API — https://developer.mozilla.org/docs/Web/API/Push_APIMDN:Notifications — https://developer.mozilla.org/docs/Web/API/Notifications_APIweb.dev:Push — https://web.dev/articles/push-notifications-overview

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部
1.948632s