Last active
December 16, 2015 20:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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