我在Chrome浏览器中收到通知时,想要播放推送通知的声音。 我已经在我的网站上设置了浏览器推送通知,但它不会发出声音。 我已经尝试了调整声音选项,但没有听到声音。
option = {
'body' : 'This is tst Description',
'icon' : 'icon.png',
'silent' : 'false',
'sound' : 'bell.mp3'
}
如果还有其他方法可以在通知中播放声音,请告诉我。
我在Chrome浏览器中收到通知时,想要播放推送通知的声音。 我已经在我的网站上设置了浏览器推送通知,但它不会发出声音。 我已经尝试了调整声音选项,但没有听到声音。
option = {
'body' : 'This is tst Description',
'icon' : 'icon.png',
'silent' : 'false',
'sound' : 'bell.mp3'
}
如果还有其他方法可以在通知中播放声音,请告诉我。
myAudio = new Audio("alert_tone.mp3");
var notification = new Notification("Hi there", options);
myAudio.play();
目前浏览器不支持Notification.sound。 请参见https://developer.mozilla.org/en/docs/Web/API/notification
web-extension
background_pages。它将监听service worker
并播放音频。其中一种简单的方法是在service worker
中请求URL,并使用chrome.webRequest
在后台脚本中捕获它,如link中所述。chrome.webRequest.onBeforeRequest.addListener(
callback, filter, opt_extraInfoSpec);
如在本 帖子 中提到的
更新 如在本 链接 中提到的
Service Workers 需要安全来源,例如 HTTPS。 chrome-extension:// 页面不是 HTTP/HTTPS,但是是安全的,因此这个更改成为允许扩展注册 Service Worker 的必要步骤。
"chrome-extension" 被添加为允许 service workers 的协议。
在 serviceworker 脚本
中
首先需要在 manifest.json 中注册您的扩展名
chrome.webRequest.onBeforeRequest.addListener
在后台脚本中进行捕捉 background.js
中播放声音正如@Alexander所提到的,目前MDN不支持声音。
如下链接所示:
截至撰写本文时,尚无浏览器支持此选项。
https://developers.google.com/web/fundamentals/push-notifications/display-a-notification#sound