使用新的chrome.notifications
API,我无法从扩展程序中收到通知以显示。即使最基本的通知也无法为我显示,但我没有收到任何错误,并且回调函数已正确执行。
{
"name": "notify",
"version": "0.0.0",
"manifest_version": 2,
"permissions": [
"notifications"
],
"background": {
"scripts": ["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!'); });
});
检查背景页面时,可以看到“已创建!” 在控制台中,但我从未在桌面上收到通知。我尝试了一堆不同的优先级值无济于事。我究竟做错了什么?
不幸的是,由于我尚未诊断出一个错误,因此从控制台抑制了有关chrome.notifications的详细错误消息。未显示您的通知的原因是,该通知未提供必需的“ iconUrl”参数。当我在扩展程序的后台页面中尝试以下操作时,我已经安装了:
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是值得的:
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); });
这将向您显示实际上是必需的属性,而缺少一个。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句