3

I want to play audio like whatsapp web push notification or telegram web notification. I already able received push notification in service worker, the problem is how to play audio when push notification received ?

I already read the documentation of Notification API on here :https://developer.mozilla.org/en-US/docs/Web/API/notification/sound

Based the documentation, Notification have parameter Sound (Notification.sound) .

I already implement that parameter to my syntax :

self.registration.showNotification(title, {
  body: body,
  icon: icon,
  tag:tag,
  data: data,
  sound: 'http://127.0.0.1/myfolder/audio/alert.mp3'
})

But still can't play the audio when receied push message in service worker, how to make play ?

Thanks.

2 Answers2

1

As indicated on that MDN page:

Note: This property is not currently supported in any browser.

I don't know of any browser vendors who are planning on adding in support at this time; the way you'd normally determine that is to visit the feature status page for a given browser and see what's on the public list. Chrome and Firefox, which are the most relevant browsers when it comes to service worker notifications, official feature trackers are hosted at

One alternative would be to use a custom vibration pattern, which is support in Chrome for Android as of version 45.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
  • Ok thanks you. But how way web whatsapp can do that ? Did you know ? – Rizky Muhammad Aug 31 '16 at 09:46
  • I'm not a WhatsApp user, and I'm not familiar with how their web app is designed. If they're running code from the context of an open web page, rather than from a service worker, then the full range of audio-related APIs are available. Perhaps they only play audio when there's a page open? – Jeff Posnick Aug 31 '16 at 14:25
  • Hi @JeffPosnick, can you look at this? https://stackoverflow.com/questions/62261805/how-to-play-sound-in-service-worker-or-even-vibrate-in-chrome – Giorgi Lagidze Jun 13 '20 at 15:04
0

In our solution we created a Google Chrome Extension which listens to the events from Service Worker and plays the audio. For the Service Worker -> background-script communication we use this "hack": How can Chrome extension background script communicate with web page service worker?

It is also possible to listen on push directly with the extension (using GCM).

Community
  • 1
  • 1
Martin Ždila
  • 2,998
  • 3
  • 31
  • 35