Skip to content

Instantly share code, notes, and snippets.

@mbrevoort
Last active December 16, 2015 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbrevoort/5493447 to your computer and use it in GitHub Desktop.
Save mbrevoort/5493447 to your computer and use it in GitHub Desktop.
A basic AnguarlJS webkit notification service that only notifies when the window is not focused.
app.factory('desktopNotification', function ($window, $document) {
var focused = true;
var onFocus = function () { focused = true; };
var onBlur = function () { focused = false; };
if (/*@cc_on!@*/false) { // check for Internet Explorer
$document.onfocusin = onFocus;
$document.onfocusout = onBlur;
} else {
$window.onfocus = onFocus;
$window.onblur = onBlur;
}
return {
notify: function (title, body) {
if (!focused && $window.webkitNotifications && $window.webkitNotifications.checkPermission() === 0) {
var notification = $window.webkitNotifications.createNotification('', title, body);
notification.onclick = function (x) {
window.focus();
this.cancel();
}
notification.show();
}
},
requestPermission: function () {
if ($window.webkitNotifications) {
$window.webkitNotifications.requestPermission();
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment