Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to play sound in service worker or even vibrate in chrome?

I am using service workers. I need it for push notifications. What I want to achieve is that when I recieve notification, it should play a sound or vibrate at least.

self.registration.showNotification(title, {
   body: body,
   icon: './assets/images/icons/icon_144x144.png',
   vibrate: [200, 100, 200, 100, 200, 100, 200],
});

I am having this piece of code upon receiving a notification.

Problem 1: No vibration happens at all. Then I went ahead of a little bit debugging.

https://googlechrome.github.io/samples/notifications/vibrate.html This DOESN'T vibrate my phone.

https://googlechrome.github.io/samples/vibration/index.html This DOES vibrate my phone.

Why doesn't it vibrate from the first link? I am testing all this on android chrome.

Problem 2: Looks like on firefox, it does have a default sound. Why doesn't google have the same sound? It just goes silent. No way I can play sound ? at least I want to vibrate it...

like image 727
Giorgi Lagidze Avatar asked Jun 08 '20 12:06

Giorgi Lagidze


1 Answers

As shown here, the vibrate property of showNotification() is no longer supported on Android devices from Android O onward, regardless of the Chrome version, which is likely why the first link doesn't work for you.

However, you can use the vibrate() method (which is what the second link is doing), which is supported on Chrome versions after version 32. However note that after Chrome 60, this method will only work in response to a user gesture. (see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/vibrate) for more details.

Adding some more history here:

According to the Chromium devs:

Unfortunately we've had to deprecate this starting Android O a few years ago - introduction of notification channels means that attention drawers (vibration, sound and all) are now per channel rather than per notification, which Chrome doesn't enable developers to configure.

Addressing that has zero interest from other vendors unfortunately.

like image 141
lfalin Avatar answered Sep 20 '22 01:09

lfalin