調(diào)用瀏覽器系統(tǒng)通知API
常用于用戶最小化瀏覽器,或者切換到其他應(yīng)用時(shí),網(wǎng)頁(yè)想通知用戶最新消息的場(chǎng)景。例如對(duì)話新消息。
接收一個(gè)msg對(duì)象,對(duì)象包含通知展示的標(biāo)題,內(nèi)容等,也可添加一些其他信息,例如圖片
function alter_news(msg) {
? ? if (window.Notification && Notification.permission !== undefined) {
? ? ? ? Notification.requestPermission((s)=>{
? ? ? ? if (s == 'granted') {
? ? ? ? ? ? let n = new Notification(msg.title,{
? ? ? ? ? ? ? ? body: msg.message
? ? ? ? ? ? })
? ? ? ? ? ? n.onclick = ()=>{
? ? ? ? ? ? ? ? window.open(window.location.href,'_bank').close()
? ? ? ? ? ? ? ? n.close()
? ? ? ? ? ? }
? ? ? ? }else {
? ? ? ? ? ? window.alert(msg.message)
? ? ? ? }
? ? })
? ? }
}
通知窗口我綁定了一個(gè)事件,用于使最小化的瀏覽器直接打開,由于沒查到直接喚醒瀏覽器的API,所以做了一個(gè)平替方案,利用了window.open的“_bank”參數(shù),會(huì)直接喚醒瀏覽器,再及時(shí)關(guān)閉新打開頁(yè)面,效果一樣。
詳細(xì)請(qǐng)參考:https://developer.mozilla.org/zh-CN/docs/Web/API/Notifications_API