概述通知权限与后台显示配合能改善到达反馈。本文给出权限请求与 SW 中触发通知的最小实现。权限与页面入口async function ensureNotifyPermission() {
if (!('Notification' in window)) return false;
if (Notification.permission === 'granted') return true;
const res = await Notification.requestPermission();
return res === 'granted';
}
SW 中显示与记录self.addEventListener('message', e => {
if (e.data && e.data.type === 'notify') {
self.registration.showNotification('提示', { body: e.data.text || '' });
}
});

发表评论 取消回复