javascript - 当浏览器在后台运行时收到通知时使用 Firebase 播放声音

标签 javascript notifications firebase-cloud-messaging service-worker

我想知道如何使用 Firebase Cloud Messaging (FCM) 播放声音 当 Chrome、Firefox 等网络浏览器在后台运行时收到通知消息。

在后台运行时通知 enter image description here 要在应用程序处于后台时接收消息,在 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/

相关文章:

javascript - 是否可以对 JQuery 中文本区域中输入的文本进行实时检测?

javascript - 将 React 升级到 0.13.2 会导致 : "Uncaught TypeError: Cannot read property ' _currentElement' of null"

android - 是否可以检查通知是否可见或已取消?

flutter - 在 Flutter 中调试 firebase_messaging 中的 onLaunch 回调

ios - 在 Firebase FCM 上检测前景推送点击事件

php 如果在 javascript 中

javascript - 隐藏表格内容/剪切angular js,Html中的内容

java - 在服务器中从 gcm 迁移到 fcm

iOS - 要求在初始拒绝后启用推送通知

android 如何在点击通知时打开上一个 Activity