HTML5 Zone is brought to you in partnership with:

I am mastered in computer application (MCA)and working as Architect in one of the leading software services company in India. I have more than 7 years of experience in software development. Sumit is a DZone MVB and is not an employee of DZone and has posted 24 posts at DZone. You can read more from them at their website. View Full User Profile

Cordova (formerly PhoneGap) - Alert, Confirm, Vibrate and Beep in Windows Phone

09.04.2012
| 3510 views |
  • submit to reddit

In this article I will talk about the Notification feature of Cordova or PhoneGap in Window Phone. Notification has alert, confirm, beep and vibrate methods.

Let's write some code.

Step 1: Place below code in index.html located under www.

<!DOCTYPE html>
<html>
<head>
   <title>Getting Started Sample</title>
   <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
   <script type="text/javascript" charset="utf-8">
      var init = function init() {
         // Wait for Cordova to load
         document.addEventListener("deviceready", onDeviceReady, false);
         // Cordova is ready
         function onDeviceReady() {
         }
      };
      window.onload = init;
   </script>
</head>
<body>
   <div id="alertAction"></div>
   <div id="confirmAction"></div>
</body>
</html>

Step 2: Place below code inside onDeviceReady method to display alert message box.

function showAlert() {
   document.getElementById("alertAction").innerHTML = "Alert Button Pressed.";
}

navigator.notification.alert(
   'Alert Mesage!', 
   showAlert, 
   'Alert Title', 
   'Alert Button'
);

showAlert is callback method which triggers on click of the Alert Button.

Cordova or PhoneGap - Alert

On click of Alert Button of alert message box, showAlert callback will be executed.

PhoneGap or Cordova - alert

alert message box is asyncrhonous, it won't block the code execution.

 

 

Step 3: Now place below code inside onDeviceReady method to display confirm message box.

 

function showConfirm(buttonIndex) {
   document.getElementById("alertAction").innerHTML = "Button index is " + buttonIndex;
}

navigator.notification.confirm(
   'Confrim Message!', 
   showConfirm, 
   'Confirm Title', 
   'Button 1, Button 2, Button3'
);

 

showConfirm is callback method which triggers on click of button on confirm message box. It takes buttonIndex argument which is index of the pressed button in confirm message box.


Cordova or PhoneGap - Confirm


On click of second button (Button 2) of confirm message box, showConfirm method will be executed. In the below screen shot you will notice the the index is 2 because second button is pressed.


PhoneGap or Cordova-confirm

Like alert, confirm message box is also asyncrhonous, it won't block the code execution.

Step 4: Place below one line of code inside onDeviceReady method to make the device vibrate. It takes time as milliseconds, 1000 milliseconds equals 1 seconds.

 

navigator.notification.vibrate(500);

Step 5: Place below one line of code inside onDeviceReady method to play beep sound. It takes argument as number of times to beep.

 

navigator.notification.beep(3);

This ends the article of alert, confrim, vibrate and beep notification using Cordova or PhoneGap in Windows Phone.

 

Published at DZone with permission of Sumit Dutta, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)