我想知道如何使用 Firebase Cloud Messaging (FCM) 播放声音 当 Chrome、Firefox 等网络浏览器在后台运行时收到通知消息。
在后台运行时通知
要在应用程序处于后台时接收消息,在 FCM 中,调用服务 worker firebase-messaging-sw.js
setBackgroundMessageHandler
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
...
return self.registration.showNotification(notificationTitle, notificationOptions);
});
但是,音频对象 window.AudioContext
无法在 service worker 中调用。另外,通知属性Notification.sound
当前不支持任何浏览器。
我想知道如何在应用程序处于后台时使用 FCM 播放声音。
最佳答案
虽然目前不支持声音,但您应该能够在服务工作人员内部显示通知时让用户的设备振动。这可能足以引起他们的注意。 (如果你真的需要......)
这是来自 live sample 的摘录:
registration.showNotification('Vibration Sample', {
body: 'Your title here.',
icon: 'path/to/icon.png',
vibrate: [200, 100, 200, 100, 200, 100, 200],
tag: 'vibration-sample'
});
关于javascript - 当浏览器在后台运行时收到通知时使用 Firebase 播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46784303/