我尝试使用chrome扩展程序制作桌面通知。 我会这样工作 - 当用户访问推荐页面时,它将显示。
background.js
function show() {
var notification = window.webkitNotifications.createNotification(
'48.png',
'YOUR VISIT PAGE http://stackoverflow.com/!'
);
notification.show();
}
// Conditionally initialize the options.
if (!localStorage.isInitialized) {
localStorage.isActivated = true; // The display activation.
localStorage.frequency = 1; // The display frequency, in minutes.
localStorage.isInitialized = true; // The option initialization.
}
function checkForValidUrl(tabId, changeInfo, tab) {
if (tab.url.indexOf('stackoverflow') > -1) {
if (window.webkitNotifications) {
if (JSON.parse(localStorage.isActivated)) {
show();
}
}
}
}
chrome.tabs.onUpdated.addListener(checkForValidUrl);
的manifest.json
{
"name": "YouVisit",
"version": "0.1",
"description":
"Show desktop notification when user visit page",
"icons": {"48": "48.png"},
"permissions": [
"notifications",
"tabs"
],
"background": { "scripts": ["background.js"] },
"manifest_version": 2,
"web_accessible_resources": [
"48.png"
]
}
为什么这段代码不起作用的任何想法?有人能给我一些文献来证明它是正确的吗?
答案 0 :(得分:1)
您未能为createNotification()
函数提供正确的参数:
根据 the docs :
// Create a simple text notification:
var notification = webkitNotifications.createNotification(
'48.png', // icon url - can be relative
'Hello!', // notification title
'Lorem ipsum...' // notification body text
);