chrome扩展:来自后台脚本的桌面通知

时间:2014-06-13 16:59:29

标签: macos google-chrome-extension notifications

我尝试修改原始通知示例,但我发现旧的webit通知不再有效。所以我改为chrome通知。基本上,我仍然重复使用这样的背景页面:

"permissions": [
    "notifications",
    "http://*/*" 
  ],
  "options_page": "options.html",
  "background": { "scripts": ["background.js"], "persistent": false },

任何background.js都是这样的:

console.log("Before create " + notID + " notification");
chrome.notifications.create("id"+notID++, options, creationCallback);


function creationCallback(ID) {
  console.log("Succesfully created " + ID + " notification");
}

看起来没有正确配置,我看不到通知。我看到以下日志:

Before create 0 notification background.js:87
Succesfully created undefined notification background.js:92

我还有什么想念吗?

由于

更新:这是修改后的background.js

window.addEventListener('load', function() {
  var opt = {
    iconUrl: "48.png",
    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!');console.log(chrome.runtime.lastError)});
});

这是完成的manifest.json:

{
  "name": "Notification Demo",
  "version": "1.1",
  "description":
    "Shows off desktop notifications, which are \"toast\" windows that pop up on the desktop.",
  "icons": {"16": "16.png", "48": "48.png", "128": "128.png"},
  "permissions": [
    "notifications",
    "http://*/*" 
  ],
  "options_page": "options.html",
  "background": { "scripts": ["background.js"]},
  "manifest_version": 2,

  // crbug.com/134315
  "web_accessible_resources": [
    "48.png"
  ]
}

1 个答案:

答案 0 :(得分:0)

如果发生任何事情,可能会发生错误。我们最近开始记录未经检查的chrome.runtime.lastError的错误,所以可能尝试更新版本的Chrome。