Chrome扩展程序丰富的通知未显示

时间:2014-08-31 04:04:34

标签: javascript google-chrome-extension notifications

在我的代码中,我希望能够创建通知,但以下代码无效。我似乎无法在任何地方找到教程,API对我来说毫无意义。这是我的代码,我感谢所有的帮助。谢谢!

function notify(string) {
    chrome.notifications.create(string)
}
notify("Testing")

在我的清单中:

"permissions": [ "unlimitedStorage", "tabs", "notifications"]

2 个答案:

答案 0 :(得分:4)

免责声明:我不是Chrome开发人员。

根据文档,notifications.create方法需要在对象中提供一些选项,第一个参数是通知的ID。

function notify(title, callback) {

    var options = {
        title: title,
        message: "Message goes here"
        type: "basic", // Which type of notification to display - https://developer.chrome.com/extensions/notifications#type-TemplateType
        iconUrl: "someimage.jpg" // A URL to the sender's avatar, app icon, or a thumbnail for image notifications.
    };

    // The first argument is the ID, if left blank it'll be automatically generated.
    // The second argument is an object of options. More here: https://developer.chrome.com/extensions/notifications#type-NotificationOptions
    return chrome.notifications.create("", options, callback);

}

notify("Testing", function(notification){
    // Do whatever you want. Called after notification is created.
});

编辑:然后猜猜所有参数都是必需的。更新了以上代码。这应该适用于简单的通知,但是当你开始做任何高级操作时,你应该read the docs

答案 1 :(得分:0)

似乎iconUrl是造成问题的原因 使用chrome.extension.getURL(“ / favicon-32x32.png”)

function notify(title, callback) {
  var options = {
    title: title,
    message: "Message goes here",
    type: "basic", // Which type of notification to display - https://developer.chrome.com/extensions/notifications#type-TemplateType
    iconUrl: chrome.extension.getURL("/favicon-32x32.png") // A URL to the sender's avatar, app icon, or a thumbnail for image notifications.
  };

  // The first argument is the ID, if left blank it'll be automatically generated.
  // The second argument is an object of options. More here: https://developer.chrome.com/extensions/notifications#type-NotificationOptions
  return chrome.notifications.create("demo", options, callback);
}