11

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...

Giorgi Lagidze
  • 773
  • 4
  • 24

1 Answers1

3

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.

Community
  • 1
  • 1
lfalin
  • 4,219
  • 5
  • 31
  • 57
  • Thanks for this nice explanation. But i need to use vibrate in service worker. it seems like I can't. what's the suggestion/solutions for that? – Giorgi Lagidze Jun 20 '20 at 09:11
  • Unfortunately, post Chrome 60, vibrate() can only be used in response to a user gesture. – lfalin Jun 20 '20 at 13:11
  • So, it means that if notification appears, i can't play the sound/vibrate... the only workaround is when it arrives, i post a message to a client and do the sound there but that means user has to have a website opened, right ? – Giorgi Lagidze Jun 22 '20 at 08:41
  • See this post: https://stackoverflow.com/questions/56388258/what-constitues-user-gesture for what constitutes a "user gesture". – lfalin Jun 22 '20 at 14:41
  • @GiorgiLagidze did you find the solution ? I encountered the same problem but still can't find answers – Garsivirus Oct 28 '20 at 08:36
  • 1
    @Garsivirus What I did was when service worker receives a notification, I post a message to all clients and in the src code, i listen to this message and when received, I play a sound. This won't work sometimes since it requites a user gesture first, but it's somehow close – Giorgi Lagidze Nov 09 '20 at 14:30
  • I had the same problem with the gesture requirement, it's troublesome... – Garsivirus Nov 09 '20 at 22:11
  • Thank you very much for your reply @GiorgiLagidze – Garsivirus Nov 09 '20 at 22:12