Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,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).
100 changes: 72 additions & 28 deletions notify.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,77 @@
/*! Copyright (c) 2016 Rene Tanczos <[email protected]> - 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(callback) {
if (!'Notification' in window) {
return;
}
if (Notification.permission !== 'granted') {
Notification.requestPermission(callback);
}
}
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);
}
};

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;
}

}
else if (Notification.permission === 'denied') {
(options && options.ondenied) && options.ondenied(this);
}
};

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);
2 changes: 0 additions & 2 deletions notify.min.js

This file was deleted.