Javascript 如何向网站添加推送通知?
我正在尝试向我的网站添加推送通知。当用户登录时,他们将收到一个通知,提醒他们添加结果。我从chrome弹出窗口中找到了代码,但它似乎不起作用Javascript 如何向网站添加推送通知?,javascript,php,html,push-notification,Javascript,Php,Html,Push Notification,我正在尝试向我的网站添加推送通知。当用户登录时,他们将收到一个通知,提醒他们添加结果。我从chrome弹出窗口中找到了代码,但它似乎不起作用 <script> Notification.requestPermission(function(status) { console.log('Notification permission status:', status); }); function displayNotification() { if(Notification
<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(){
if(Notification.permission==“已授予”){
jQuery(“#通知头”).hide();
通知=新通知(“新博客可用”{
正文:“演示段落”,
图标:“bg-Img.png”,
标签:'12222'
});
notify.onclick=function(){
notify.close();
}
}
您需要在函数末尾添加一个大括号。您可以查看。此外,您永远不会调用显示通知,您需要使用以下方法:
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();
}
}
}
好吧,至少你的通知里没有结尾}function@Rantanen,我认为它更像是一个桌面通知,而不是像那样的弹出警告。@EntiendNull谢谢我添加了一个额外的}但它只问我是否允许通知,它实际上没有显示!在检查是否已授予权限之前,您正在请求权限。只需将您的代码替换为mdn上的示例。。。在检查是否已授予权限之前,您正在请求权限。那又怎样?如果它已经被批准了,那就没什么区别了。对不起,我错了。我做了一个小的编辑,以能够删除伊托克,没有问题。