NotificationOptions.vibrate Sample

Available in Chrome 45+ | View on GitHub | Browse Samples

When displaying a system notification your device may vibrate to alert the user. You can specify the vibration pattern that should be used in the same way as for the Vibration API.

When creating the notification, just set the 'vibrate' property of the notification options to be a Vibration API pattern.

Chrome for Android only supports showing persistant notifications, which are associated with a service worker to handle the click event. We must first register a worker, and create the notification using the showNotification method of the registration object.

JavaScript Snippet

navigator.serviceWorker.register('sw.js');

function showNotification() {
  Notification.requestPermission(function(result) {
    if (result === 'granted') {
      navigator.serviceWorker.ready.then(function(registration) {
        registration.showNotification('Vibration Sample', {
          body: 'Buzz! Buzz!',
          icon: '../images/touch/chrome-touch-icon-192x192.png',
          vibrate: [200, 100, 200, 100, 200, 100, 200],
          tag: 'vibration-sample'
        });
      });
    }
  });
}