桌面通知栏(让你的h5最小化也可以通知信息,并返回通知来源)

576 阅读1分钟

假如有一个需求,需要本地h5页面打开PC通知栏,请试试Notification~

支持浏览器谷歌、火狐、safari 不支持IE

            // 有桌面通知栏功能
            if (window.Notification) {
                Notification.requestPermission((result) => {
                    // 拒绝打开通知栏
                    if(result.data == 'denied') return;
                    // 同意打开通知栏
                    if (result == 'granted') {
                        let notification = new Notification('您有新消息', {
                            body: `您打开了xxx桌面弹窗`, // 正文
                            icon: 'http://www.xxx.com' // 图片
                        })
                        
                        // 点击通知栏事件
                        notification.onclick = () => {
                            // 点击后执行返回通知栏发起页
                            window.focus();
                        }
                    }
                })
            }