Skip to content

Instantly share code, notes, and snippets.

@ssskip
Created April 26, 2014 14:04
Show Gist options
  • Save ssskip/11320838 to your computer and use it in GitHub Desktop.
Save ssskip/11320838 to your computer and use it in GitHub Desktop.
implement html5 web notification support chrome Safari
;(function (window) {
"use strict";
void function () {
var notify= null, i = -1, EVENT_TYPE = {show: 1, error: 1, close: 1, click: 1}, DesktopNotify = {isSupport: function () {
return "Notification" in window || "webkitNotifications" in window
}, show: function (e, i, n) {
notify = this.create(e, i, n), notify.show()
}, showHTML: function (e) {
notify = this.createHTML(e), notify.show()
}, hide: function (callback) {
notify && notify.close && t.close(), callback && callback()
}, destroy: function () {
notify = null, i = -1
}, checkPermission: function () {
return i = webkitNotifications.checkPermission()
}, isPermitted: function () {
return 0 === this.checkPermission()
}, requestPermission: function (e) {
this.isPermitted() ? e && e() : webkitNotifications.requestPermission(e)
}, create: function (e, t, i) {
return webkitNotifications.createNotification(e, t, i)
}, createHTML: function (e) {
return webkitNotifications.createHTMLNotification(e)
}, on: function (e, callback) {
EVENT_TYPE[e] && notify && notify.addEventListener(e, callback, !1)
}, un: function (e, callback) {
EVENT_TYPE[e] && notify && notify.removeEventListener(e, callback, !1)
}};
window || (window = {}), window.DesktopNotify = DesktopNotify
}()
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment