概述通知权限与后台显示配合能改善到达反馈。本文给出权限请求与 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 || '' });

}

});

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部