Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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.

 

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}