How to allow vibration on notifications using Cordova

Posted By : Akhil Dhiman | 04-Dec-2014
Here I will show you how one can add vibration to application on getting some notifications.

Description

When application receives any notification then we can vibrate our device to make user aware of notification. This vibration can be done for 1 second as well as we can make vibration to be execute for multiple times. i.e. we can make vibration after fixed intervals of time untill user views the notification.
 

Plugin to use:

I used plugin named Cordova Vibration Plugin you can use the following link to check the plugin at Github https://git-wip-us.apache.org/repos/asf?p=cordova-plugin-vibration.git
 
To add this plugin in your project follow the following procedure:
Using terminal go in your project directory, from inside your Cordova project you have to enter the following command:
cordova plugin add org.apache.cordova.vibration
 

 

There are three types of vibration functionalities that are provided by this plugin:

  1. Standard vibrate
  2. Vibrate with a pattern
  3. Cancel vibration
  • Standard vibrate
 
This is the used for standard vibration that is used to vibrate a device for given amount of time.
Below are the methods to call vibration onto device.
 // Vibrate for 5 seconds
navigator.notification.vibrate(5000);

// Vibrate for 5 seconds
navigator.notification.vibrate([5000]);
 
  • Vibrate with a pattern
 
Vibration with a pattern is method to vibrate device in some format. This is only supported for android and windows phone.
Below is the method to call vibration with pattern.
navigator.notification.vibrate([1000, 1000, 3000, 1000, 5000]);
 

 

The above example will working as follow:

  1. Vibrate for 1 second
  2. Wait for 1 second
  3. Vibrate for 3 seconds
  4. Wait for 1 second
  5. Vibrate for 5 seconds
  • Cancel vibration
 
Cancel Vibration is used to cancel all vibrations immediately. This is not supported in iOS. To stop vibration we need call the below method.
 navigator.notification.cancelVibration();
 

Thanks

About Author

Author Image
Akhil Dhiman

Akhil is an iPhone and Android application developer with experience in PhoneGap and Swift(Native iOS). Akhil has good experience working with JavaScript, jQuery and Underscore as well.

Request for Proposal

Name is required

Comment is required

Sending message..