我有一个有聊天功能的网络应用程序,当我在页面或背景或其他窗口选项卡中收到消息时,我想激活我在JS中的音频。所以它永远不会播放音频,只有当你在窗口时。
这就是我所拥有的: 火力的消息-sw.js
messaging.setBackgroundMessageHandler(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
const title = ' - ¡Mensaje Nuevo!';
const options = {
body: 'conductor: '+payload.data.chofer_id+' - '+payload.data.mensaje,
icon: '/icono.png',
};
return self.clients.matchAll().then(all => all.forEach(client => {
client.postMessage("Responding to ");
}));
//return self.registration.showNotification(title, options);
});
我的js在我的页面中(这里我有我的音频播放):
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('firebase-messaging-sw.js')
.then(function (reg) {
// registration worked
console.log('Registration succeeded. Scope is ' + reg.scope);
}).catch(function (error) {
// registration failed
console.log('Registration failed with ' + error);
});
}
navigator.serviceWorker.addEventListener('message', function handler(event) {
console.log('Received a message from service worker: ', event.data);
$.playSound('Sounds/Tethys.mp3');
});