Skip to content

Instantly share code, notes, and snippets.

@ozexpert
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozexpert/9259545 to your computer and use it in GitHub Desktop.
Save ozexpert/9259545 to your computer and use it in GitHub Desktop.
AngularJS / Cordova Notification Service for device & web (Cordova Ver 3.3.0)
YourModule.factory('Notification', function($rootScope){
return {
alert: function(message, callback){
if(navigator.notification !== undefined){
navigator.notification.alert(message,
function(){
$rootScope.$apply(callback());
});
} else {
alert(message);
callback();
}
},
confirm: function(message, callback){
if(navigator.notification !== undefined){
var callbackWrapper = function(buttonIndex){
if(buttonIndex == 1){
callback();
}
};
navigator.notification.confirm(message,
function(buttonIndex){
$rootScope.$apply(callbackWrapper(buttonIndex));
});
} else {
var trueOrFalse = confirm(message);
if(trueOrFalse){
callback();
}
}
},
prompt: function(message, title, callback){
//navigator.notification.prompt(message, promptCallback, [title], [buttonLabels], [defaultText])
if(navigator.notification !== undefined){
var callbackWrapper = function(results){
if(results.buttonIndex == 1){
callback(results.input1);
}
};
navigator.notification.prompt(message,
function (results) {
$rootScope.$apply(callbackWrapper(results));
}, title);
} else {
var sessionTitle = prompt(message);
callback(sessionTitle);
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment