Skip to content

Instantly share code, notes, and snippets.

@serbanghita
Last active May 4, 2016 15:25
Show Gist options
  • Save serbanghita/75de9758eada643ce9823dfa02730066 to your computer and use it in GitHub Desktop.
Save serbanghita/75de9758eada643ce9823dfa02730066 to your computer and use it in GitHub Desktop.
Cordova Dialogs (Notifications)
var writeResult = function (property, value) {
var $result = document.createElement('div');
$result.innerHTML = '<b>' + property + ':</b> ' + value;
document.body.appendChild($result);
};
var createButton = function (label, callback) {
var $button = document.createElement('button');
$button.innerHTML = label;
$button.addEventListener('click', callback, false);
document.body.appendChild($button);
};
var dialogAlert = function () {
navigator.notification.alert(
'This is an alert',
function () {
console.log('Alert was closed.');
},
'This is the title',
'Ok'
);
};
var dialogConfirm = function () {
navigator.notification.confirm(
'Whould you like to accept this confirm notification?',
function (buttonIndex) {
},
'This is the title',
['Yes', 'No']
);
};
var dialogPrompt = function () {
navigator.notification.prompt(
'Please enter your name',
function (results) {
console.log(results.buttonIndex, results.input1);
},
'This is the title',
['Register', 'Cancel'],
'Serban Ghita'
);
};
var beep = function (times) {
navigator.notification.beep(times);
};
var onCordovaReady = function() {
for (var i in navigator.notification) {
if (!navigator.notification.hasOwnProperty(i)) {
continue;
}
writeResult('notification property', i);
}
createButton('Open alert', dialogAlert);
createButton('Open confirm', dialogConfirm);
createButton('Open prompt', dialogPrompt);
createButton('Beep once', function () { beep(1); });
createButton('Beep twice', function () { beep(2); });
createButton('Open 2 alerts', function () {
dialogAlert();
dialogAlert();
});
createButton('Open 2 prompts', function() {
dialogPrompt();
dialogPrompt();
});
createButton('Beep 30 times', function () { beep(30); });
};
document.addEventListener('deviceready', onCordovaReady, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment