gpt4 book ai didi

javascript - 通知权限总是被拒绝

转载 作者:搜寻专家 更新时间:2023-11-01 04:13:13 27 4
gpt4 key购买 nike

我正在使用 Notification.permission 检查浏览器是否允许通知。

我的检查通知权限的代码如下。

    // Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
var prm = Notification.permission;
if (prm == 'default' || prm == 'denied') {
console.log("permission denied or default");
}else{
console.log("permission granted");
}

此代码在我的 localhost 中运行良好,但当我尝试在生产中使用时,它总是会给出拒绝状态。我的浏览器通知设置是在此站点上始终允许enter image description here但我不知道是什么问题。需要帮助。

最佳答案

它说的很简单:[Deprecation] 可能不再从不安全的来源使用 Notification API。您应该考虑将您的应用程序切换到安全来源,例如 HTTPS。参见 google's advice更多细节。(匿名)@ ?message=unique-identifier=123:25?message=unique-identifier=123:26 被拒绝你 lint 应该看起来像这样:[linkExample1][2] - 用于 index.html 或 [linkExampe2][2]


Here is the link 它不能联机运行 但您可以在本地服务器上运行它只需创建任何 html(htm) 文件并HTTPS 中运行它协议(protocol),然后在您的 URL 中选择 Allow 选项: Hear is what i mean

One small gotcha do not use private(incognito mode) for this lab - for security reason push notifications are not supported in private or incognito mode

    let dnperm = document.getElementById('dnperm');
let dntrigger = document.getElementById('dntrigger');

dnperm.addEventListener('click', function(e){
e.preventDefault();

if(!window.Notification){
alert("Notification not supported!");
}else{
Notification.requestPermission().then(function(permission) {
console.log(permission);
if(permission === 'denied'){
alert('You Have Denied Notification!');
}else if(permission === 'granted'){
alert('You Have Granted notification.');
}
})
}
});

// simulate

dntrigger.addEventListener('click', function(e){
let notify;

e.preventDefault();

console.log(Notification.permission);

if(Notification.permission === 'default'){
alert('Please allow notification before doing this');
}else {
notify = new Notification('New Message From Romzik', {
body: 'How are you today? Is it really is a lovely day.',
icon: 'img/msg-icon.png',
tag: 'unique-identifier=123' // msg-id
});

notify.onclick = function (ev) {
console.log(this);
window.location = '?message=' + this.tag;
}
}


})
<body>
<a href="" id="dnperm">Request permission</a>
<a href="" id="dntrigger">Trigger</a>
</body>

关于javascript - 通知权限总是被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47447637/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com