首页 > 解决方案 > 有没有办法在网络推送通知(Service Worker)中播放自定义通知声音?

问题描述

我查看了很多资源,但找不到积极的结果。我想在收到推送通知时播放一些声音。

self.addEventListener("push", (e) => {
  const data = e.data.json();
  // console.log("Push Recieved...");
  self.registration.showNotification(data.title, {
    body: data.message,
    icon: data?.icon,
    image: data?.media,
    url: data?.url,
    data: { url: data?.url },
    vibrate: [
      100, 30, 100, 30, 100, 30, 200, 30, 200, 30, 200, 30, 100, 30, 100, 30,
      100,
    ],
  });
  const playSound = new Audio(
    "https://notificationsounds.com/storage/sounds/file-b8_discreet-song.mp3"
  );
  playSound.play();
  alert("NEW ORDER RECEIVED");
  navigator.vibrate([
    100, 30, 100, 30, 100, 30, 200, 30, 200, 30, 200, 30, 100, 30, 100, 30, 100,
  ]);
});

我也试过这种方式。但发现一个错误,说音频不是函数。

标签: javascriptpush-notificationprogressive-web-appsservice-worker

解决方案


推荐阅读