-
Notifications
You must be signed in to change notification settings - Fork 22
/
notify.js
188 lines (146 loc) · 4.15 KB
/
notify.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
(function(window){
/**
* @brief Notifier
*
* @param type 1. 一直提示 2,更新提示内容 3, 控制显示上限。超过个数删除最早的 4,超时更新
* @param param 本参数在type为3,4时才有效, type为3表示可以最多显示通知的个数 4, 表示多少秒后删除了
*
* @return
*/
function Notifier() {};
window.Notifier = Notifier;
type = 1;
queue = [];
t = 5;
c = 3;
_notifier = undefined ;
if(undefined !== window.webkitNotifications) {
_notifier = window.webkitNotifications;
}else if (undefined !== window.Notification) {
_notifier = window.Notification;
} else {
console.log('error not found notification!')
}
window.Notifier.ModelAll = function() {
type = 1;
}
window.Notifier.ModelUpdate = function() {
type = 2;
}
window.Notifier.ModelCount = function(ct) {
if(ct !== undefined) c = ct;
type = 3;
}
window.Notifier.ModelTimeout = function(timeout) {
if(timeout !== undefined) t = timeout;
type = 4;
}
window.Notifier.HasSupport = function() {
if(undefined === _notifier) {
return false;
}
return true;
}
window.Notifier.GetPermission = function() {
return _notifier.checkPermission();
}
window.Notifier.IsGetPermission = function() {
return (_notifier === 0);
}
window.Notifier.Disable = function() {
return (_notifier.checkPermission === 2);
}
window.Notifier.RequestPermission = function(cb) {
_notifier.requestPermission(function() {
if(cb) {cb(_notifier.checkPermission() == 0)}
});
}
//type = 1;关闭上一个
window.Notifier.Close = function(type) {
if(type = 1) {
tmp = queue.pop();
} else {
tmp = queue.shift();
}
_closeItem(tmp);
}
window.Notifier.ClosePre = function () {
tmp = queue.pop();
_closeItem(tmp);
}
window.Notifier.CloseLast = function () {
tmp = queue.shift();
_closeItem(tmp);
}
window.Notifier.CloseAll = function() {
while(queue.length > 0) {
var tmp = queue.shift();
_closeItem(tmp);
}
}
window.Notifier.Notify = function(icon, title, body, fnClick) {
if (this.IsGetPermission() == 0) {
var popup = _createNotificationAndShow(icon, title, body);
if(undefined == popup) {
return false;
}
switch(type) {
case 2:
if(queue.length > 0) {
tmp = queue.pop();
_closeItem(tmp);
}
break;
case 3:
while(queue.length >= c) {
tmp = queue.shift();
_closeItem(tmp);
}
break;
case 4:
setTimeout(function(){_closeItem(popup);}, t*1000);
break;
}
var q = queue;
popup.onclose = function(){
var cur = q.indexOf(popup);
if(cur >= 0) {
q.splice(cur, 1);
}
};
popup.onclick = function() {
if ( fnClick != undefined && typeof fnClick == "function") {
fnClick()
}
};
queue.push(popup);
return true;
} else {
RequestPermission();
}
return false;
}
function _createNotificationAndShow(icon, title, body) {
if(undefined != window.webkitNotifications && _notifier.name === window.webkitNotifications.name) {
var n = _notifier.createNotification(icon, title, body);
n.show();
return n;
}else if (undefined !== window.Notification && _notifier.name === window.Notification.name) {
return new _notifier(title, {icon:icon, body: body});
} else {
console.log('error not found notification!')
alert(title +"\n\n"+body);
return undefined;
}
}
function _closeItem(n) {
if(undefined == n) {
return
}
if(n.cancel) {
n.cancel();
} else {
n.close();
}
}
})(window);