setTimeout(function(){chrome.notifications.create({
type: "basic",
title: "Test Notification",
message: "testing"
});
},5000);
我试图在页面加载5秒后发出通知,但它根本没有发出通知。
有一个TypeError出现了,但我不明白为什么chrome.notifications会是undefined。我怎样才能解决这个问题并让它发挥作用?
我在看了这里之后编写了这个脚本。
编辑:这可能有帮助吗?我用的是烧瓶,我希望这样做。
发布于 2020-06-26 22:53:25
您正在调查错误的文档。您发布的链接是您可以从谷歌网络商店下载的应用程序/浏览器扩展的文档。这是适合你的合适的文档。顺便说一句,别忘了先申请许可,否则,你的通知就没有机会被解雇了!祝你在黑客方面好运;)
在浏览器中尝试的代码片段
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
// At last, if the user has denied notifications, and you
// want to be respectful there is no need to bother them any more.
}我在上面提供的02.07.2020代码片段编辑将不会再次请求许可,以防您先前拒绝它。这意味着,如果您在StackOverflow上打开一个控制台,在此之前您拒绝了对通知的访问,在这里尝试之后,您将看不到任何结果。此片段将请求发出通知,您不关心以前是否拒绝它。
function notifyMe() {
if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
} else {
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
}https://stackoverflow.com/questions/62603492
复制相似问题