简体   繁体   English

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

[英]Chrome extension rich notifications not showing

In my code, I want to be able to create notifications, but the following code is not working.在我的代码中,我希望能够创建通知,但以下代码不起作用。 I can't seem to find a tutorial anywhere and the API is making no sense to me.我似乎无法在任何地方找到教程,而且 API 对我来说毫无意义。 Here is my code, I appreciate all help.这是我的代码,感谢所有帮助。 Thanks!谢谢!

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

In my manifest:在我的清单中:

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

Disclaimer: I'm not a Chrome developer.免责声明:我不是 Chrome 开发人员。

According to the docs, the notifications.create method requires a few options to be supplied in an object, and the first parameter is an ID for the notification.根据文档,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.
});

Edit: Guess all arguments are required then.编辑:猜猜所有参数都是必需的。 Updated code above.更新了上面的代码。 This should work fine for a simple notification, but when you start doing anything advanced you should read the docs .这对于简单的通知应该可以正常工作,但是当您开始执行任何高级操作时,您应该阅读 docs

It seems the iconUrl was creating the issue use chrome.extension.getURL("/favicon-32x32.png")似乎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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM