javascript - 如何向网站添加推送通知?

标签 javascript php html push-notification

我正在尝试向我的网站添加推送通知。当用户登录时,他们将收到一条通知,提醒他们添加结果。我发现了 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/

相关文章:

Javascript - 检测何时反射(reflect)样式更改?

php - 如何调整单元格的宽度,使其不会在 Spreadsheet_Excel_Writer 中剪切单词

php - 将两个 Xpath 组合成一个循环?

php - 我也想在 php/mysql 中导入 excel 文件和一些图像。有人有解决办法吗?

JavaScript INDEX_SIZE_ERR : DOM Exception 1

javascript - Rails 选择表单 : displaying and using select form value before submit?

javascript - 如何将 gridstack.js 添加到 Ractive.js?

javascript - 从 URL 批量下载文件 如何添加 HTTP header ?

html - 如何使图像元素随窗口缩放而不是重新定位

html - 为什么我的 CSS 类不起作用