如何在收到网络推送通知时播放自定义声音

4 javascript push-notification reactjs

我实现了网络推送通知。通知即将到来,但我想播放我添加的自定义通知声音,但该声音不起作用默认系统窗口声音即将到来,我想播放此声音。我添加了代码,让我知道为什么这个通知声音没有播放接收

self.addEventListener('push', async function (event) {
  const data = event.data.json();
  console.log(data);
  const title = 'Sound Notification';
  const options = {
    sound: '../public/messageNotification.mp3',
  };

  try {
      registration.showNotification(title, options);
  } catch (e) {
    registration.showNotification(title, options);
  }
});
Run Code Online (Sandbox Code Playgroud)

小智 5

我认为你可以使用 HTMLAudioElement 来达到他的目的。例如:

let sound: HTMLAudioElement;
sound = new Audio();
sound.src = '../public/messageNotification.mp3';
sound.load();
sound.play();
Run Code Online (Sandbox Code Playgroud)