-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpush_notification.js
80 lines (78 loc) · 2.43 KB
/
push_notification.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
(function() {
(function($) {
var notify_methods;
notify_methods = {
create_notification: function(options) {
return new Notification(options.title, options);
},
close_notification: function(notification, options) {
return setTimeout(notification.close.bind(notification), options.closeTime);
},
set_default_icon: function(icon_url) {
return default_options.icon = icon_url;
},
isSupported: function() {
if (("Notification" in window) && (Notification.permission !== "denied")) {
return true;
} else {
return false;
}
},
permission_request: function() {
if (Notification.permission === "default") {
return Notification.requestPermission();
}
}
};
return $.extend({
notify: function(body, arguments_options) {
var notification, options;
if (arguments.length < 1) {
throw "Notification: few arguments";
}
if (typeof body !== 'string') {
throw "Notification: body must 'String'";
}
var default_options = {
'title': "Notification",
'body': "Body",
'closeTime': 5000,
'icon' : ""
};
default_options.body = body;
options = $.extend(default_options, arguments_options);
if (notify_methods.isSupported()) {
notify_methods.permission_request();
notification = notify_methods.create_notification(options);
notify_methods.close_notification(notification, options);
return {
click: function(callback) {
notification.addEventListener('click', function() {
return callback();
});
return this;
},
show: function(callback) {
notification.addEventListener('show', function() {
return callback();
});
return this;
},
close: function(callback) {
notification.addEventListener('close', function() {
return callback();
});
return this;
},
error: function(callback) {
notification.addEventListener('error', function() {
return callback();
});
return this;
}
};
}
}
});
})(jQuery);
}).call(this);