我在向wicket网站添加基本桌面通知时遇到问题。基于https://developer.mozilla.org/en-US/docs/Web/API/Notification处的示例代码,我在页面中覆盖了renderHead(IHeaderResponse)方法,以便将以下内容添加到页面中:
<script type="text/javascript" id="desktopNotification-init">
/*<![CDATA[*/
function notifyMe(){
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}else if (Notification.permission === "granted") {
var notification = new Notification("You have a system notification");
}else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if(!('permission' in Notification)) {
Notification.permission = permission;
}
if (permission === "granted") {
var notification = new Notification("You have a system notification");
}
});
}
}
/*]]>*/
</script>
然后我有一个按钮,在单击时添加对该函数的调用:
public void onClick( AjaxRequestTarget p_target, WebMarkupContainer p_menuItemComponent ) {
if( p_target != null ) {
p_target.appendJavaScript( "notifyMe();" );
}
}
这适用于Firefox,但点击该按钮对Chrome无效。这似乎是权限请求的问题,因为如果我将设置更改为始终允许通知,它可以正常工作。我错过了javascript的问题吗?