vue實現(xiàn)瀏覽器桌面通知的示例代碼
瀏覽器桌面通知:當(dāng)瀏覽器最小化,或者切換到其他標(biāo)簽頁不在當(dāng)前系統(tǒng)頁面,或在其他頁面時依然可以顯示通知
*使用前注意:生產(chǎn)環(huán)境地址必須為https協(xié)議,開發(fā)環(huán)境可以用localhost IP地址,且必須允許顯示通知才能顯示桌面通知
*存在兼容性問題,不同系統(tǒng)不同瀏覽器甚至不同版本瀏覽器效果略有不同
方案一: H5 JavaScript Web Notification API
Notification官網(wǎng)
目前Notification除了IE瀏覽器不支持外, 其他瀏覽器都已支持桌面通知,移動端瀏覽器基本都未支持
// 判斷是否支持顯示
showJudge (data) {
if (!('Notification' in window)) {
alert('抱歉,此瀏覽器不支持桌面通知!')
}
// granted: 用戶允許該網(wǎng)站發(fā)送通知 default: 默認值,用戶還未選擇 denied: 用戶拒絕該網(wǎng)站發(fā)送通知
// Notification.permission === 'granted' 可用于檢測用戶通知權(quán)限
Notification.requestPermission().then((result) => {
if (result === 'denied') {
console.log('用戶拒絕')
return
} else if (result === 'default') {
console.log('用戶未授權(quán)')
return
}
this.sendMesgToDesk(data)
})
},
// 顯示消息通知
sendMesgToDesk (data) {
let notification = null
let title = data.data.auditTitle
let str1 = data.data.applicant + ' ' + data.data.applyTime
let options = {
tag: data.data.wfFormId, // 多條消息時tag相同只顯示一條通知,需要顯示多條時tag一定要不同,(谷歌每次只能顯示一條,火狐可以顯示多條)
body: str1, // 通知主體
data: { // 可以放置任意數(shù)據(jù),方便后續(xù)使用
content: data.data,
originUrl: `http://localhost:8889/#/home`
},
requireInteraction: true, // 不自動關(guān)閉通知 默認值為false,通知會在三四秒之后自動關(guān)閉,(谷歌有用,火狐依然會自動關(guān)閉)
image: require('../../../assets/img/AAA.png'), // 通知上方可顯示需要展示的大圖
icon: require('../../../assets/img/XXX.png') // 通知圖標(biāo) 默認是瀏覽器圖標(biāo)
}
notification = new Notification(title, options)
// 事件處理
notification.onclick = ({ currentTarget: { data } }) => {
// notification.close() 單個通知關(guān)閉
window.focus()
// 默認進入系統(tǒng)之前打開的頁面,也可以這里自定義進入的頁面
window.location.href = data.originUrl
}
notification.onshow = () => {
console.log('通知顯示了')
}
notification.onclose = () => {
console.log('通知被關(guān)閉了')
}
notification.onerror= () => {
console.log('遇到錯誤')
}
},
方案二: push.js 工具 (基于notification)
push官網(wǎng)
一、引入
1.script引入方式
<script src="https://cdnjs.cloudflare.com/ajax/libs/push.js/0.0.11/push.min.js"></script>
2.npm安裝引入
npm install push.js --save
引入
import Push from 'push.js' // 如果全局使用在main.js引入后,進行掛載: Vue.prototype.Push = Push
二、主要代碼
// 手動獲取用戶桌面通知權(quán)限
if (this.Push.Permission.GRANTED) { // 判斷當(dāng)前是否有權(quán)限,沒有則手動獲取
this.Push.Permission.request()
}
// 監(jiān)聽瀏覽器 當(dāng)前系統(tǒng)是否在當(dāng)前頁
document.addEventListener('visibilitychange', () => {
if (!document.hidden) { // 處于當(dāng)前頁面
// 關(guān)閉之前的消息通知,清空
this.Push.clear()
this.notificationArr = []
}
})
// 發(fā)送 瀏覽器 桌面通知
showDeskNotify (data) {
if (!this.Push.Permission.has()) {
alert('抱歉,此瀏覽器不支持桌面通知!')
return
}
// 關(guān)閉之前的消息通知
this.Push.clear()
let title = '消息通知(' + (this.auditMessageArr.length + 1) + '條未讀)'
this.Push.create(title, {
tag: data.data.wfFormId,
body: '類型:' + data.data.auditTitle + '\n時間:' + data.data.applyTime,
requireInteraction: true,
icon: require('../../../assets/img/XX.png'),
onClick: () => {
window.focus()
// this.close() // 單個關(guān)閉
this.Push.clear() // 全部關(guān)閉
// window.location.href = data.originUrl
}
})
},
方案三: iNotify.js JS
JS 實現(xiàn)瀏覽器的 title 閃爍、滾動、聲音提示、chrome、Firefox、Safari等系統(tǒng)通知
1.npm安裝引入
npm install title-notify --save
2.主要代碼
var iNotify = new iNotify().init()
//推薦下面寫法
var iNotify = new iNotify({
message: '有消息了。',//標(biāo)題
effect: 'flash', // flash | scroll 閃爍還是滾動
openurl:"http://www.bing.com", // 點擊彈窗打開連接地址
onclick:function(){ //點擊彈出的窗之行事件
console.log("---")
},
//可選播放聲音
audio:{
//可以使用數(shù)組傳多種格式的聲音文件
file: ['msg.mp4','msg.mp3','msg.wav']
//下面也是可以的哦
//file: 'msg.mp4'
},
//標(biāo)題閃爍,或者滾動速度
interval: 1000,
//可選,默認綠底白字的 Favicon
updateFavicon:{
// favicon 字體顏色
textColor: "#fff",
//背景顏色,設(shè)置背景顏色透明,將值設(shè)置為“transparent”
backgroundColor: "#2F9A00"
},
//可選chrome瀏覽器通知,默認不填寫就是下面的內(nèi)容
notification:{
title:"通知!",//設(shè)置標(biāo)題
icon:"",//設(shè)置圖標(biāo) icon 默認為 Favicon
body:'您來了一條新消息'//設(shè)置消息內(nèi)容
}
})
3.其他
判斷瀏覽器彈框通知是否被阻止。
iNotify.isPermission()
播放聲音
iNotify.player() // 自動播放 iNotify.loopPlay()
停止播放
iNotify.stopPlay()
設(shè)置播放聲音URL
iNotify.setURL('msg.mp3')// 設(shè)置一個
iNotify.setURL(['msg.mp3','msg.ogg','msg.mp4']) // 設(shè)置多個
添加計數(shù)器
iNotify.addTimer()
清除計數(shù)器
iNotify.clearTimer()
到此這篇關(guān)于vue實現(xiàn)瀏覽器桌面通知的示例代碼的文章就介紹到這了,更多相關(guān)vue 瀏覽器桌面通知內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何在 ant 的table中實現(xiàn)圖片的渲染操作
這篇文章主要介紹了如何在 ant 的table中實現(xiàn)圖片的渲染操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue-week-picker實現(xiàn)支持按周切換的日歷
這篇文章主要為大家詳細介紹了vue-week-picker實現(xiàn)支持按周切換的日歷,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06
Avue自定義formslot調(diào)用rules自定義規(guī)則方式
在Avue框架中,使用formslot自定義表格列時可能會遇到無法調(diào)用Avue的自定義校驗規(guī)則的問題,這通常發(fā)生在嘗試通過formslot自定義設(shè)置列的場景中,解決這一問題的一個有效方法是將自定義列與Avue的校驗規(guī)則通過特定方式連接起來2024-10-10
Vue-Router實現(xiàn)頁面正在加載特效方法示例
這篇文章主要給大家介紹了利用Vue-Router實現(xiàn)頁面正在加載特效方法示例,文中給出了詳細的示例代碼,相信對大家具有一定的參考價值,有需要的朋友們下面來一起看看吧。2017-02-02
Vue2.0實現(xiàn)調(diào)用攝像頭進行拍照功能 exif.js實現(xiàn)圖片上傳功能
這篇文章主要為大家詳細介紹了Vue2.0實現(xiàn)調(diào)用攝像頭進行拍照功能,以及圖片上傳功能引用exif.js,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04

