【发布时间】:2016-03-13 20:44:59
【问题描述】:
您好,我想向访问我网站的客人发送桌面通知。
所以我需要为此使用 Chrome api。因为只有chrome浏览器才有桌面通知。
这是我发送通知的代码。我检查了这个Chrome Desktop Notification Example
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="check.js"></script>
</head>
<body>
<input type="button" onclick="notifyMe()" value="Send Notification"></input>
</body>
</html>
当我单击按钮时,会为我显示一条通知。但是我想创建一个管理面板,当我单击按钮时,应该为访问我的网站并允许通知的所有用户显示通知。 例如,在 5 月 21 日,我需要发送“今天是 5 月 21 日,您可以买到便宜货!”之类的通知。必须为所有允许通知的用户显示此消息。谢谢。
【问题讨论】:
标签: javascript google-chrome notifications