当浏览器在后台运行时收到通知时,使用Firebase播放声音 [英] Play a sound with Firebase when browser receives a notification while running in the background
问题描述
我想知道当Chrome浏览器,Firefox等网络浏览器在后台运行时收到通知消息时,如何使用Firebase云消息传递(FCM)
播放声音。
I would like to know how to play a sound with Firebase Cloud Messaging (FCM) when the web browser such as Chrome, Firefox, etc. receives a notification message while running in the background.
通知
< img src =https://i.stack.imgur.com/bTCQm.pngalt =在此处输入图像说明>
要在应用程序处于后台时收到消息,请在FCM,在服务工作者中调用 setBackgroundMessageHandler
firebase-messaging-sw.js
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
...
return self.registration.showNotification(notificationTitle, notificationOptions);
});
但音频对象 window.AudioContext
可以不要在服务工作者中调用。此外,通知属性 通知。任何浏览器目前都不支持声音
。
However, Audio Object window.AudioContext
can not be called in service worker. In addition, The notification property Notification.sound
is not currently supported in any browser.
我想知道当应用程序在后台时如何使用FCM播放声音。
I would like to know how to play a sound with FCM when the app is in the background.
推荐答案
虽然目前不支持声音,但您应该能够在从服务工作者内部显示通知时振动用户的设备。这可能足以引起他们的注意。 (如果你真的需要...)
While sounds are not currently supported, you should be able to vibrate a user's device when showing a notification from inside of a service worker. That might be sufficient to get their attention. (If you really need to...)
以下是实时样本:
registration.showNotification('Vibration Sample', {
body: 'Your title here.',
icon: 'path/to/icon.png',
vibrate: [200, 100, 200, 100, 200, 100, 200],
tag: 'vibration-sample'
});
这篇关于当浏览器在后台运行时收到通知时,使用Firebase播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!