微弱提示音

This commit is contained in:
2023-10-25 22:56:51 +08:00
parent ae224bd983
commit 4ea9a51740
1 changed files with 16 additions and 2 deletions

View File

@ -134,10 +134,23 @@ export default class Chat {
Notification.requestPermission() 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) { async 通知栏消息(data) {
console.log('通知栏消息', data) console.log('通知栏消息', data)
const { name, text, avatar } = data const { name, text, avatar } = data
const icon = 'https://webrtc.satori.love/favicon.ico'
// 如果页面可见或且浏览器不在前台运行, 则发送通知 // 如果页面可见或且浏览器不在前台运行, 则发送通知
if (document.visibilityState === 'visible') return console.log('页面可见') if (document.visibilityState === 'visible') return console.log('页面可见')
// 如果用户不允许通知,则不发送通知 // 如果用户不允许通知,则不发送通知
@ -147,7 +160,7 @@ export default class Chat {
await Notification.requestPermission() await Notification.requestPermission()
} }
if (Notification.permission === 'granted') { if (Notification.permission === 'granted') {
const notification = new Notification(name, { body: text, icon:avatar }) const notification = new Notification(name, { body: text, icon: avatar })
notification.onclick = () => { notification.onclick = () => {
window.focus() window.focus()
notification.close() notification.close()
@ -372,5 +385,6 @@ export default class Chat {
this.添加元素(data) this.添加元素(data)
this.存储消息(save) this.存储消息(save)
this.通知栏消息(data) this.通知栏消息(data)
this.播放提示音()
} }
} }