-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotificationManager.js
248 lines (227 loc) · 6.34 KB
/
NotificationManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* @author Ratan Phayade
* @class
* Manages the browser notification on all the browser.
* supports: Chrome, Firefox and safari (Mobile and desktop version)
*/
var NotificationManager = (function(){
/**
* @private
* Holds all the valid event list
*/
var actionEvents = [
'onclick',
'onshow',
'onerror',
'onclose'
];
/**
* Initialize the new instance of NotificationManager
* @constructs NotificationManager
* @param {string} title
* @param {string} icon
*/
function NotificationManager(title, icon){
this.title = title;
this.icon = icon;
this.timeout = 5000;
this.silent = true;
this.lang = 'eng';
this.init();
};
/**
* Checks the browser support for Notificaiton
* Also checks for the permission and prompts
* @name NotificationManager#init
* @function
*/
this.init = function () {
if(this.isBrowserSupported)
if(this.hasPermission !== "granted")
this.requestPermission();
};
/**
* Create or alters the property of the object
* @name NotificationManager#setProperty
* @function
*/
this.setProperty = function(property, value) {
this[property] = value;
return this;
};
/**
* clears particular property of the object
* @name NotificationManager#clearProperty
* @function
*/
this.deleteProperty = function(property) {
this[property] = null;
return this;
};
/**
* Sets the timeout value in mili sec
* @name NotificationManager#setTimeout
* @function
*/
this.setTimeout = function(timeout) {
this.timeout = timeout;
return this;
};
/**
* Sets the icon image for the notification
* @name NotificationManager#setIcon
* @function
*/
this.setIcon = function(iconUrl) {
this.icon = iconUrl;
return this;
};
/**
* Sets data property of notification
* @name NotificationManager#setData
* @function
*/
this.setData = function(data) {
this.data = data;
return this;
};
/**
* Sets title property of notification.
* which will be displayed on notification
* @name NotificationManager#setTitle
* @function
*/
this.setTitle = function(title) {
this.title = title;
return this;
};
/**
* Sets language property of notification
* @name NotificationManager#setLang
* @function
*/
this.setLang = function(lang){
this.lang = lang;
return this;
};
/**
* Sets the silent property of notificaiton
* Based on the value and browser compatability
* @name NotificationManager#needSound
* @function
*/
this.needSound = function(value) {
this.silent = !value;
return this;
};
/**
* Adds listener to the event occuring on notification.
* Valid events : ['onclick', 'onshow', 'onclose', 'onerror']
* @name NotificationManager#addListener
* @function
*/
this.addListener = function(event, callback){
this[event] = callback;
return this;
};
/**
* clears all event listener.
* @name NotificationManager#resetListeners
* @function
*/
this.resetListeners = function(){
actionEvents.forEach(function(element){
delete this[element];
});
return this;
};
/**
* Removes listener to the event occuring on notification.
* Valid events : ['onclick', 'onshow', 'onclose', 'onerror']
* @name NotificationManager#deleteListener
* @function
*/
this.deleteListener = function(event){
delete this[event];
return this;
};
/**
* Gives the browser compatability status
* @name NotificationManager#isBrowserSupported
* @function
*/
this.isBrowserSupported = function() {
return (!!(window.Notification // W3C Specification
|| win.webkitNotifications // old WebKit Browsers
|| navigator.mozNotification // Firefox for Android and Firefox OS
)
);
}();
/**
* Initializes the notifier instance
* @name NotificationManager#initiateNotifier
* @function
*/
this.initiateNotifier = function() {
this.notifire = (window.Notification // W3C Specification
|| win.webkitNotifications // old WebKit Browsers
|| navigator.mozNotification // Firefox for Android and Firefox OS
);
return this.notifire;
}();
/**
* Gives the current browser premission for site
* @name NotificationManager#hasPermission
* @function
*/
this.hasPermission = function(){
return this.notifire.permission;
}();
/**
* Requests the browser for notification permission for the current site.
* (If browser has notification support)
* @name NotificationManager#requestPermission
* @function
*/
this.requestPermission = function() {
return this.notifire.requestPermission();
};
/**
* Binds all the listener Event to the notification
* @name NotificationManager#bindEvents
* @function
*/
this.bindEvents = function(notification){
actionEvents.forEach(function(element) {
delete notification[element];
if(this.hasOwnProperty(element)){
notification[element] = this[element];
}
}, this);
return notification;
};
/**
* Sets the property of notificaiton and triggers a notification
* @name NotificationManager#notify
* @function
*/
this.notify = function(message) {
try {
var n = new Notification(
this.title,
{
body: message,
icon: this.icon,
lang: this.lang
}
);
this.bindEvents(n);
setTimeout(
n.close.bind(n),
this.timeout
);
} catch (err){
console.log(err.message);
}
};
});