gpt4 book ai didi

javascript - 在本地主机上启用浏览器通知

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

在控制台中写入时,如何在使用本地主机时启用浏览器通知 Notification.requestPermission();除了在控制台中写的任何常规站点上什么都没有出现 Notification.requestPermission();请求许可。

如何启用它来测试我的代码?非常感谢你...

最佳答案

您的代码应该可以正常工作..可能是您遗漏了其他内容..请尝试此代码..

用法

sendNotification({
title: 'New Notification',
message: 'Your message goes here',
icon:'https://cdn2.iconfinder.com/data/icons/mixed-rounded-flat-icon/512/megaphone-64.png',
clickCallback: function () {
alert('do something when clicked on notification');
}
});

函数

function sendNotification (data) {
if (data == undefined || !data) { return false }
var title = (data.title === undefined) ? 'Notification' : data.title
var clickCallback = data.clickCallback
var message = (data.message === undefined) ? 'null' : data.message
var icon = (data.icon === undefined) ? 'https://cdn2.iconfinder.com/data/icons/mixed-rounded-flat-icon/512/megaphone-64.png' : data.icon
var sendNotification = function (){
var notification = new Notification(title, {
icon: icon,
body: message
})
if (clickCallback !== undefined) {
notification.onclick = function () {
clickCallback()
notification.close()
}
}
}

if (!window.Notification) {
return false
} else {
if (Notification.permission === 'default') {
Notification.requestPermission(function (p) {
if (p !== 'denied') {
sendNotification()
}
})
} else {
sendNotification()
}
}
}

关于javascript - 在本地主机上启用浏览器通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45263299/

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