我正在尝试向我的网站添加推送通知。当用户登录时,他们将收到一条通知,提醒他们添加结果。我发现了 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>
最佳答案
您需要在函数末尾添加花括号。你可以看看this example .此外,您永远不会调用 displayNotification
,您需要使用它:
Notification.requestPermission(function(status) {
console.log('Notification permission status:', status);
displayNotification();
});
function displayNotification() {
if(Notification.permission == "granted"){
$('#notification-head').hide();
let notify = new Notification("New Blog Available", {
body:"Demo paragraph",
icon:'bg-Img.png',
tag:'12222'
});
notify.onclick = function(){
notify.close();
}
}
}
关于javascript - 如何向网站添加推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49880007/