Alf*_*aAs 5 javascript firebase service-worker firebase-cloud-messaging
我有一个具有聊天功能的网络应用程序,当我在页面或背景中或另一个窗口选项卡中收到消息时,我想激活我在 JS 中拥有的音频。所以它从不播放音频,只有当你在窗口上时。
这就是我所拥有的:firebase-messaging-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);
});
Run Code Online (Sandbox Code Playgroud)
我的页面中的 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');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
990 次 |
| 最近记录: |