From 2a5263b2be2ca5fea1820143a083f1f5fc8bbb7f Mon Sep 17 00:00:00 2001 From: david p Date: Thu, 14 Jan 2016 14:54:15 +0100 Subject: [PATCH 1/3] isolated scope of JsNotify object and added init method to request notification permission --- README.md | 8 +++++- notify.js | 77 ++++++++++++++++++++++++++++++++------------------- notify.min.js | 2 +- 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 99685e2..0f37554 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,13 @@ bower install js-notify ## How it works ``` -notify('title', { +var jsNotify = new JsNotify(); + +// request Notification permission +jsNotify.init(); + +// send notification +jsNotify.notify('title', { body: 'Notification Text', icon: 'path/to/image.png', onclick: function(e) {}, // e -> Notification object diff --git a/notify.js b/notify.js index 497854b..8efbcb6 100644 --- a/notify.js +++ b/notify.js @@ -1,33 +1,54 @@ /*! Copyright (c) 2016 Rene Tanczos - The MIT License (MIT) */ -var notify = function (title, options) { - var guid = function() { - function s4() { - return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); +;(function() { + + function JsNotify() { + var self = this + + self._init = function() { + if (!'Notification' in window) { + return; + } + if (Notification.permission === 'default') { + Notification.requestPermission(function () {}); + } } - return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); - }; - if (!'Notification' in window) { - return; - } - if (Notification.permission === 'default') { - Notification.requestPermission(function () { - notify(title, options); - }); - } - else if (Notification.permission === 'granted') { - opt = options || {} - opt.tag = guid() - var n = new Notification(title, opt); - n.onclick = function () { - opt.onclick && opt.onclick(this); - this.close(); - }; - n.onclose = function () { - opt.onclose && opt.onclose(this); + self._notify = function (title, options) { + var guid = function() { + function s4() { + return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); + } + return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); + }; + + if (!'Notification' in window) { + return; + } + if (Notification.permission === 'granted') { + opt = options || {} + opt.tag = guid() + var n = new Notification(title, opt); + n.onclick = function () { + opt.onclick && opt.onclick(this); + this.close(); + }; + n.onclose = function () { + opt.onclose && opt.onclose(this); + }; + } + else if (Notification.permission === 'denied') { + (options && options.ondenied) && options.ondenied(this); + } }; } - else if (Notification.permission === 'denied') { - (options && options.ondenied) && options.ondenied(this); - } -}; + + JsNotify.prototype.init = function() { + this._init() + } + + JsNotify.prototype.notify = function(title, options) { + this._notify(title, options) + } + + this.JsNotify = JsNotify +}).call(this); diff --git a/notify.min.js b/notify.min.js index 5e9535c..cfa07e5 100644 --- a/notify.min.js +++ b/notify.min.js @@ -1,2 +1,2 @@ /*! Copyright (c) 2016 Rene Tanczos - The MIT License (MIT) */ -var notify=function(i,o){var n=function(){function i(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}return i()+i()+"-"+i()+"-"+i()+"-"+i()+"-"+i()+i()+i()};if(!(!1 in window))if("default"===Notification.permission)Notification.requestPermission(function(){notify(i,o)});else if("granted"===Notification.permission){opt=o||{},opt.tag=n();var t=new Notification(i,opt);t.onclick=function(){opt.onclick&&opt.onclick(this),this.close()},t.onclose=function(){opt.onclose&&opt.onclose(this)}}else"denied"===Notification.permission&&o&&o.ondenied&&o.ondenied(this)}; +(function(){function i(){var i=this;i._init=function(){!1 in window||"default"===Notification.permission&&Notification.requestPermission(function(){})},i._notify=function(i,n){var t=function(){function i(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}return i()+i()+"-"+i()+"-"+i()+"-"+i()+"-"+i()+i()+i()};if(!(!1 in window))if("granted"===Notification.permission){opt=n||{},opt.tag=t();var o=new Notification(i,opt);o.onclick=function(){opt.onclick&&opt.onclick(this),this.close()},o.onclose=function(){opt.onclose&&opt.onclose(this)}}else"denied"===Notification.permission&&n&&n.ondenied&&n.ondenied(this)}}i.prototype.init=function(){this._init()},i.prototype.notify=function(i,n){this._notify(i,n)},this.JsNotify=i}).call(this); From 6fa946f311d43e384a53ace720eff8928d8341ad Mon Sep 17 00:00:00 2001 From: david p Date: Thu, 30 Jun 2016 17:18:57 +0200 Subject: [PATCH 2/3] added isEnabled, getStatus functions and added callback to init function --- notify.js | 33 ++++++++++++++++++++++++++++----- notify.min.js | 2 -- 2 files changed, 28 insertions(+), 7 deletions(-) delete mode 100644 notify.min.js diff --git a/notify.js b/notify.js index 8efbcb6..bc309b0 100644 --- a/notify.js +++ b/notify.js @@ -4,12 +4,12 @@ function JsNotify() { var self = this - self._init = function() { + self._init = function(callback) { if (!'Notification' in window) { return; } - if (Notification.permission === 'default') { - Notification.requestPermission(function () {}); + if (Notification.permission !== 'granted') { + Notification.requestPermission(callback); } } @@ -40,15 +40,38 @@ (options && options.ondenied) && options.ondenied(this); } }; + + self._isEnabled = function() { + if (!'Notification' in window) { + return false; + } + return Notification.permission === 'granted'; + } + + self._getStatus = function() { + if (!'Notification' in window) { + return false; + } + return Notification.permission; + } + } - JsNotify.prototype.init = function() { - this._init() + JsNotify.prototype.init = function(callback) { + this._init(callback) } JsNotify.prototype.notify = function(title, options) { this._notify(title, options) } + JsNotify.prototype.isEnabled = function() { + return this._isEnabled() + } + + JsNotify.prototype.getStatus = function() { + return this._getStatus() + } + this.JsNotify = JsNotify }).call(this); diff --git a/notify.min.js b/notify.min.js deleted file mode 100644 index cfa07e5..0000000 --- a/notify.min.js +++ /dev/null @@ -1,2 +0,0 @@ -/*! Copyright (c) 2016 Rene Tanczos - The MIT License (MIT) */ -(function(){function i(){var i=this;i._init=function(){!1 in window||"default"===Notification.permission&&Notification.requestPermission(function(){})},i._notify=function(i,n){var t=function(){function i(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}return i()+i()+"-"+i()+"-"+i()+"-"+i()+"-"+i()+i()+i()};if(!(!1 in window))if("granted"===Notification.permission){opt=n||{},opt.tag=t();var o=new Notification(i,opt);o.onclick=function(){opt.onclick&&opt.onclick(this),this.close()},o.onclose=function(){opt.onclose&&opt.onclose(this)}}else"denied"===Notification.permission&&n&&n.ondenied&&n.ondenied(this)}}i.prototype.init=function(){this._init()},i.prototype.notify=function(i,n){this._notify(i,n)},this.JsNotify=i}).call(this); From 01670290cf50ca9fdc9c332ca30ddb75b01e670f Mon Sep 17 00:00:00 2001 From: david pichsenmeister Date: Fri, 22 Jul 2016 12:29:50 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 0f37554..03554f7 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,3 @@ You just have to set a title to make it work. The json object on the second argument is optional. -If the function runs for the first time, is asks the user for permissions. - -Follow me on [Twitter @gravmatt](https://twitter.com/gravmatt).