通知在 Web 扩展中不起作用 (Firefox)

Notifications not working in web extension (Firefox)

我正在使用 Firefox 的开发者版本(版本 54.0a2,但也尝试使用普通的 Firefox 版本 51)并且我想在我的网络扩展中包含通知。

浏览器["notifications"]虽然不存在。

因为它在我的扩展中不起作用,我认为可能有什么冲突,所以我创建了这个非常基本的网络扩展。

manifest.json:

{
  "manifest_version": 2,
  "name": "Extension",
  "version": "1.0",

  "description": "...",

  "icons": {
    "48": "icons/icon-48.png"
  },

  "content_scripts": [
    {
      "matches": [
        "http://*/*",
        "https://*/*"
      ],
      "js": ["test.js"]
    }
  ],
  "permissions": ["notifications", "storage"]
}

test.js(顺便说一句,存储工作正常。)

if (browser["notifications"]) {
    console.log('Notifications exist!');

    browser.notifications.create({
      "type": "basic",
      "iconUrl": browser.extension.getURL("icons/icon-48.png"),
      "title": "test",
      "message": "test"
    });
  }
  else {
    //it always executes this part :/
    console.log('notifications do not exist');
    console.log(browser);
  }

调试插件也没有显示任何错误。

许多扩展专用的api只能运行在后台脚本中。如果您需要从内容脚本 运行 它,通常的技术是从内容脚本向后台脚本发送消息,在后台脚本中处理消息,然后从那里 运行 您想要的命令。

对于您的情况,Notification Api, one of them being notify-link-clicks-i18n 末尾有几个示例,您可以在其中查看他们如何进行消息传递以从后台脚本创建通知。