Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome扩展丰富通知不工作_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript Chrome扩展丰富通知不工作

Javascript Chrome扩展丰富通知不工作,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我有一个chrome扩展名,我想使用新的富通知。我正在尝试实现以下内容: var opt = { type: "basic", title: "New message from " + sBuffer[0] + ":", message: sBuffer[2], iconUrl: getUserIcon(sBuffer[0]) }; chrome.notificatio

我有一个chrome扩展名,我想使用新的富通知。我正在尝试实现以下内容:

var opt = {
            type: "basic",
            title: "New message from " + sBuffer[0] + ":",
            message: sBuffer[2],
            iconUrl: getUserIcon(sBuffer[0])
        };
        chrome.notifications.create("",opt,function(){});
但无论我做什么,我都会得到以下错误:

未捕获类型错误:无法调用未定义的方法“create”

我进入了
chrome://flags
并将其中包含“通知”的所有内容设置为已启用。。。我正在运行chrome 31.0.1650.57 m

这很好:

var notification =  webkitNotifications.createNotification(
    getUserIcon(sBuffer[0]),
    "New message from " + sBuffer[0] + ":",
    sBuffer[2]
);
notification.show();
它不漂亮,但可以工作(即使是高分辨率图像,图标也很小…有没有办法让图像图标变大?

顺便说一句,我的清单中有通知权限

谢谢,戴夫

编辑:包含的清单

{
"manifest_version": 2,

 "name": "Notifier",
 "description": "This extension will listen the the JS on the page and popup notifications",
 "version": "0.1",

 "permissions": [
"background","notifications"
],
"content_scripts": [
{
  "matches": ["http://MY_WEB_SITE"],
  "js": ["Notifier.js"]
}
]
}

似乎您正试图从内容脚本访问chrome.notifications
API。但它不适用于内容脚本,因此您需要在后台页面中创建通知

如果需要传递要在通知中显示的特定数据,可以使用在内容脚本和背景页面之间进行通信。
例如:


确保您在清单中设置了权限(您应该包括这些权限)。另外,
id
应该是唯一的。你说的“背景页”是什么意思?它是一个必须通过
包含的.js文件吗。不确定您正在谈论的
.js
文件是什么,以及
是如何发挥作用的。
/* In content script */
chrome.runtime.sendMessage({
    from: sBuffer[0],
    body: sBuffer[2]
});

/* In background page */
chrome.runtime.onMessage.addListener(function(msg, sender) {
    /* Verify the message's format */
    (msg.from !== undefined) || return;
    (msg.body !== undefined) || return;

    /* Create and show the notification */
    // ...your notification creation code goes here
    // (replace `sBuffer[0]`/`sBuffer[2]` with `msg.from`/`msg.body`) 
});