微弱提示音
This commit is contained in:
parent
ae224bd983
commit
4ea9a51740
18
src/chat.js
18
src/chat.js
@ -134,10 +134,23 @@ export default class Chat {
|
||||
Notification.requestPermission()
|
||||
}
|
||||
}
|
||||
async 播放提示音() {
|
||||
// 如果页面可见或且浏览器不在前台运行
|
||||
if (document.visibilityState === 'visible') return console.log('页面可见')
|
||||
// 创建一个MIDI的清脆的"叮咚"
|
||||
const context = new AudioContext()
|
||||
const o = context.createOscillator()
|
||||
const g = context.createGain()
|
||||
o.connect(g)
|
||||
o.type = 'sine'
|
||||
o.frequency.value = 400
|
||||
g.connect(context.destination)
|
||||
o.start(0)
|
||||
g.gain.exponentialRampToValueAtTime(0.00001, context.currentTime + 1)
|
||||
}
|
||||
async 通知栏消息(data) {
|
||||
console.log('通知栏消息', data)
|
||||
const { name, text, avatar } = data
|
||||
const icon = 'https://webrtc.satori.love/favicon.ico'
|
||||
// 如果页面可见或且浏览器不在前台运行, 则发送通知
|
||||
if (document.visibilityState === 'visible') return console.log('页面可见')
|
||||
// 如果用户不允许通知,则不发送通知
|
||||
@ -147,7 +160,7 @@ export default class Chat {
|
||||
await Notification.requestPermission()
|
||||
}
|
||||
if (Notification.permission === 'granted') {
|
||||
const notification = new Notification(name, { body: text, icon:avatar })
|
||||
const notification = new Notification(name, { body: text, icon: avatar })
|
||||
notification.onclick = () => {
|
||||
window.focus()
|
||||
notification.close()
|
||||
@ -372,5 +385,6 @@ export default class Chat {
|
||||
this.添加元素(data)
|
||||
this.存储消息(save)
|
||||
this.通知栏消息(data)
|
||||
this.播放提示音()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user