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:
- Standard vibrate
- Vibrate with a pattern
- 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.
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.
Below is the method to call vibration with pattern.
navigator.notification.vibrate([1000, 1000, 3000, 1000, 5000]);
The above example will working as follow:
- Vibrate for 1 second
- Wait for 1 second
- Vibrate for 3 seconds
- Wait for 1 second
- 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
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.