我注意到在 StackExchange 聊天中,我现在可以启用桌面通知(我也在 Gmail 和 Google Music 中使用)。现在我决定我希望能够在我的网站上做到这一点。我做了一些谷歌搜索,但只找到了 info on Chrome Extensions 。如何在我自己的网站中发出桌面通知?
编辑
在 Chrome 控制台中,我找到了函数 webkitNotifications.requestPermission()
,但调用它没有执行任何操作。我希望它会显示 Chrome 顶部栏,询问“您想启用 http://... 的桌面通知吗?”
最佳答案
有一个不错的Notification API tutorial at html5rocks.com 。希望这有帮助!
仅供引用,webkitNotifications.requestPermission()
在调用它时没有执行任何操作的原因是
this method should only be called while handling a user gesture, in other circumstances it will have no effect
如 Notification API Specs 中所述。因此,您可能需要通过用户手势例如鼠标单击来实现此方法,以使其生效:
<script>
function requestNotificationPermission()
{
window.webkitNotifications.requestPermission();
}
</script>
<a onclick="requestNotificationPermission(); return false;" href="#">Request notifications permission</a>
关于javascript - 如何从 Chrome 创建桌面通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11977733/