【发布时间】:2018-04-17 13:53:12
【问题描述】:
我正在尝试向我的网站添加推送通知。当用户登录时,他们将收到一条通知,提醒他们添加结果。我在 chrome 弹出窗口中找到了代码,但它似乎不起作用。
<script>
Notification.requestPermission(function(status) {
console.log('Notification permission status:', status);
});
function displayNotification() {
if(Notification.permission=="granted"){
jQuery('#notification-head').hide();
notify=new Notification("New Blog Available",{
body:"Demo paragraph",
icon:'bg-Img.png',
tag:'12222'
});
notify.onclick=function(){
notify.close();
}
}
</script>
【问题讨论】:
-
好吧,至少你的 displayNotification 函数中缺少结尾 }
-
@Rantanen,我认为它更像是桌面通知,而不是那样的弹出警报。
-
@entiendoNull 谢谢我添加了一个额外的 } 但它只询问我是否允许通知,它实际上并没有显示!
-
您在检查是否已获得许可之前请求许可。
-
只需将您的代码替换为 mdn 上的示例...developer.mozilla.org/en-US/docs/Web/API/Notifications_API/…
标签: javascript php html push-notification