首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >铬通知不是“通知”

铬通知不是“通知”
EN

Stack Overflow用户
提问于 2020-06-26 22:12:09
回答 1查看 185关注 0票数 0

代码语言:javascript
复制
setTimeout(function(){chrome.notifications.create({
    type: "basic",
    title: "Test Notification",
    message: "testing"
});
},5000);

我试图在页面加载5秒后发出通知,但它根本没有发出通知。

有一个TypeError出现了,但我不明白为什么chrome.notifications会是undefined。我怎样才能解决这个问题并让它发挥作用?

我在看了这里之后编写了这个脚本。

编辑:这可能有帮助吗?我用的是烧瓶,我希望这样做。

EN

回答 1

Stack Overflow用户

发布于 2020-06-26 22:53:25

您正在调查错误的文档。您发布的链接是您可以从谷歌网络商店下载的应用程序/浏览器扩展的文档。这是适合你的合适的文档。顺便说一句,别忘了先申请许可,否则,你的通知就没有机会被解雇了!祝你在黑客方面好运;)

在浏览器中尝试的代码片段

代码语言:javascript
复制
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上打开一个控制台,在此之前您拒绝了对通知的访问,在这里尝试之后,您将看不到任何结果。此片段将请求发出通知,您不关心以前是否拒绝它。

代码语言:javascript
复制
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!");
      }
    });
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62603492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档