Chrome通知未从Chrome扩展程序后台页面显示

Chrome通知未从Chrome扩展程序后台页面显示

问题描述:

使用新的 chrome.notifications API,我无法从自己的扩展程序中收到通知以显示.即使是最基本的通知也无法显示给我,但我没有收到任何错误,并且回调函数已正确执行.

Using the new chrome.notifications API, I am unable to get notifications from my extension to appear. Even the most basic notification fails to appear for me, but I get no errors and the callback function is properly executed.

{
  "name": "notify",
  "version": "0.0.0",
  "manifest_version": 2,
  "permissions": [
    "notifications"
  ],
  "background": {
    "scripts": ["main.js"]
  }
}

main.js

window.addEventListener('load', function() {
  var opt = {
    type: 'list',
    title: 'Primary Title',
    message: 'Primary message to display',
    priority: 1,
    items: [{ title: 'Item1', message: 'This is item 1.'},
            { title: 'Item2', message: 'This is item 2.'},
            { title: 'Item3', message: 'This is item 3.'}]
  };
  chrome.notifications.create('notify1', opt, function() { console.log('created!'); });
});

查看后台页面时,我可以看到已创建!"在控制台中,但我从未在桌面上收到通知.我尝试了一堆不同的优先级值无济于事.我在做什么错了?

When I inspect the background page, I can see "created!" in the console, but I don't ever get a notification on the desktop. I have tried a bunch of different priority values to no avail. What am I doing wrong?

不幸的是,由于我尚未诊断出一个错误,因此从控制台抑制了chrome.notifications的详细错误消息.未显示您的通知的原因是,该通知未提供必需的"iconUrl"参数.当我在扩展程序的后台页面中尝试以下操作时,我已经安装了:

Unfortunately detailed error messages for chrome.notifications have been suppressed from the console due to a bug that I haven't yet diagnosed; the reason your notification isn't being displayed is that it doesn't provide a required "iconUrl" parameter. When I tried the following in the background page of an extension I have installed:

var opt = {
  iconUrl: "http://www.google.com/favicon.ico",
  type: 'list',
  title: 'Primary Title',
  message: 'Primary message to display',
  priority: 1,
  items: [{ title: 'Item1', message: 'This is item 1.'},
        { title: 'Item2', message: 'This is item 2.'},
          { title: 'Item3', message: 'This is item 3.'}]
};
chrome.notifications.create('notify1', opt, function() { console.log('created!'); });

通知创建成功.检查chrome.runtime.lastError是值得的:

the notification is created successfully. It pays to check chrome.runtime.lastError:

var opt = {
    type: 'list',
    title: 'Primary Title',
    message: 'Primary message to display',
    priority: 1,
    items: [{ title: 'Item1', message: 'This is item 1.'},
            { title: 'Item2', message: 'This is item 2.'},
            { title: 'Item3', message: 'This is item 3.'}]
  };
  chrome.notifications.create('notify1', opt, function(id) { console.log("Last error:", chrome.runtime.lastError); });

这将向您显示实际上有一些必需属性,而其中一个缺少.

which would have shown you that in fact there are required properties and one is missing.