JS-Notification通知

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
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>html5桌面通知</title>
</head>

<body>
<input type="button" value="开启桌面通知" onclick="showDeskTopNotice('','HTML5桌面消息');">
<script>
function showDeskTopNotice(title, msg) {
var Notification =
window.Notification
|| window.mozNotification
|| window.webkitNotification;

if (Notification) {
Notification.requestPermission(function(status) {
// status默认值'default'等同于拒绝
// 'denied' 意味着用户不想要通知
// 'granted' 意味着用户同意启用通知
if ("granted" != status) {
return;
} else {
var tag = "sds" + Math.random();
var notify = new Notification(
title, {
dir: 'auto',
lang: 'zh-CN',
//实例化的notification的id
tag: tag,
//通知的缩略图
icon: 'http://www.yinshuajun.com/static/img/favicon.ico',
body: msg //通知的具体内容
}
);
notify.onclick = function() {
//如果通知消息被点击,通知窗口将被激活
window.focus();
},
notify.onerror = function() {
console.log("HTML5桌面消息出错!!!");
};
notify.onshow = function() {
setTimeout(function() {
notify.close();
}, 2000)
};
notify.onclose = function() {
console.log("HTML5桌面消息关闭!!!");
};
}
});
} else {
console.log("您的浏览器不支持桌面消息");
}
};
showDeskTopNotice("", "HTML5桌面消息")
</script>
</body>

</html>