From 4ea9a51740444af1df1da29d63a9024c9a27b883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Wed, 25 Oct 2023 22:56:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E5=BC=B1=E6=8F=90=E7=A4=BA=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/chat.js b/src/chat.js index a0adcf7..547b186 100644 --- a/src/chat.js +++ b/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.播放提示音() } } \ No newline at end of file