Javascript Can';无法在Google Chrome扩展上获得通知

Javascript Can';无法在Google Chrome扩展上获得通知,javascript,google-chrome,google-chrome-extension,notifications,Javascript,Google Chrome,Google Chrome Extension,Notifications,我正在编写Google Chrome扩展,但我无法让Chrome通知正常工作。 目标(至少出于测试目的):当用户单击右上角的扩展图标时显示通知。 发生的情况:单击扩展图标仅显示popup.html中的“测试主体”文本,但不显示任何通知。检查控制台的扩展,我可以看到回调函数的结果:Last error:undefined 编辑:我尝试使用谷歌的通知API来测试功能,但它似乎也不起作用。谈到Chrome通知上的一个bug,所以我担心这可能就是问题所在。如有任何关于此错误的信息,我们将不胜感激。 这是

我正在编写Google Chrome扩展,但我无法让Chrome通知正常工作。
目标(至少出于测试目的):当用户单击右上角的扩展图标时显示通知。
发生的情况:单击扩展图标仅显示
popup.html
中的“测试主体”文本,但不显示任何通知。检查控制台的扩展,我可以看到回调函数的结果:
Last error:undefined

编辑:我尝试使用谷歌的通知API来测试功能,但它似乎也不起作用。谈到Chrome通知上的一个bug,所以我担心这可能就是问题所在。如有任何关于此错误的信息,我们将不胜感激。
这是我的代码:
manifest.json:

{
  "name": "Test Extension",
  "description": "Test description",
  "version": "1.0",
  "manifest_version": 2,
  "icons": { "16": "images/icon16.png", "48": "images/icon48.png", "128": "images/icon128.png" },
  "content_scripts": [
    {
      "matches": ["*://*.amazon.com/*buy*"],
      "js": ["js/content.js"]
    }
  ],
  "browser_action": {
    "default_popup": "html/popup.html",
    "default_title": "Test Extension"
  },
  "background": {
    "scripts": ["js/background.js"]
  },
  "permissions": [
    "notifications"
  ],
  "web_accessible_resources": ["images/icon48.png"]
}
popup.html:

{
  "name": "Test Extension",
  "description": "Test description",
  "version": "1.0",
  "manifest_version": 2,
  "icons": { "16": "images/icon16.png", "48": "images/icon48.png", "128": "images/icon128.png" },
  "content_scripts": [
    {
      "matches": ["*://*.amazon.com/*buy*"],
      "js": ["js/content.js"]
    }
  ],
  "browser_action": {
    "default_popup": "html/popup.html",
    "default_title": "Test Extension"
  },
  "background": {
    "scripts": ["js/background.js"]
  },
  "permissions": [
    "notifications"
  ],
  "web_accessible_resources": ["images/icon48.png"]
}

Nubank信贷控制
试验体

content.js:

{
  "name": "Test Extension",
  "description": "Test description",
  "version": "1.0",
  "manifest_version": 2,
  "icons": { "16": "images/icon16.png", "48": "images/icon48.png", "128": "images/icon128.png" },
  "content_scripts": [
    {
      "matches": ["*://*.amazon.com/*buy*"],
      "js": ["js/content.js"]
    }
  ],
  "browser_action": {
    "default_popup": "html/popup.html",
    "default_title": "Test Extension"
  },
  "background": {
    "scripts": ["js/background.js"]
  },
  "permissions": [
    "notifications"
  ],
  "web_accessible_resources": ["images/icon48.png"]
}
chrome.runtime.sendMessage({
url:window.location.href
});
background.js:

{
  "name": "Test Extension",
  "description": "Test description",
  "version": "1.0",
  "manifest_version": 2,
  "icons": { "16": "images/icon16.png", "48": "images/icon48.png", "128": "images/icon128.png" },
  "content_scripts": [
    {
      "matches": ["*://*.amazon.com/*buy*"],
      "js": ["js/content.js"]
    }
  ],
  "browser_action": {
    "default_popup": "html/popup.html",
    "default_title": "Test Extension"
  },
  "background": {
    "scripts": ["js/background.js"]
  },
  "permissions": [
    "notifications"
  ],
  "web_accessible_resources": ["images/icon48.png"]
}
chrome.runtime.onMessage.addListener(函数(){
常量选项={
类型:“基本”,
iconUrl:chrome.extension.getURL(“../images/icon48.png”),
标题:“这是标题”,
消息:“这是通知的主要消息”,
};
create(“notifId”,options,function(){console.log(“Last error:”,chrome.runtime.lastError);});
});

chrome.notifications API仅在后台上下文中可用,请参阅:以获取完整的解决方案。

实际上,通知也可以在popup.js中触发。据我所知,问题在于如何调用chrome.notifications.create()。 第一个参数是可选的,但如果仍要传递它,则它应该是有效的通知id,而不仅仅是“notifId”

出于测试目的,这应该足够了:

manifest.json

{
  "name": "Test Extension",
  "description": "Test description",
  "version": "1.0",
  "manifest_version": 2,
  "browser_action": {
    "default_popup": "html/popup.html",
    "default_title": "Test Extension"
  },
  "permissions": [
    "notifications"
  ]
}
{
  "name": "Test Extension",
  "description": "Test description",
  "version": "1.0",
  "manifest_version": 2,
  "browser_action": {
    "default_title": "Test Extension"
  },
  "background": {
    "scripts": ["js/background.js"]
  },
  "permissions": [
    "notifications"
  ]
}
popup.js

const选项={
类型:“基本”,
iconUrl:“../images/icon48.png”,
标题:“Popup.js”,
消息:“您好,来自popup.js!”
};
chrome.notifications.create(选项);
或者,如果使用background.js:

manifest.json

{
  "name": "Test Extension",
  "description": "Test description",
  "version": "1.0",
  "manifest_version": 2,
  "browser_action": {
    "default_popup": "html/popup.html",
    "default_title": "Test Extension"
  },
  "permissions": [
    "notifications"
  ]
}
{
  "name": "Test Extension",
  "description": "Test description",
  "version": "1.0",
  "manifest_version": 2,
  "browser_action": {
    "default_title": "Test Extension"
  },
  "background": {
    "scripts": ["js/background.js"]
  },
  "permissions": [
    "notifications"
  ]
}
background.js

const选项={
类型:“基本”,
iconUrl:“../images/icon48.png”,
标题:“Background.js”,
消息:“您好,来自background.js!”
};
chrome.browserAction.onClicked.addListener(函数(){
chrome.notifications.create(选项);
});

这是否回答了您的问题?你说得对,我改变了这一点,并更新了上面的代码。但它仍然不起作用。你应该可以通过重新安装来解决这个错误。它对我不起作用。我得到了确认和通知id,但没有收到任何通知。您的代码中可能存在路径不正确的问题?此外,在服务器上重新加载扩展也很重要chrome://extensions/ 应用更改后的页面。你试过哪个例子?用popup.js还是background.js?无论如何,这里有一个我刚刚双重测试过的工作示例:这是Windows通知的一个问题,它们被禁用了。扩展正在发送通知,但Windows没有显示这些通知。非常感谢。