[英]Can't get notifications to work on Google Chrome Extension
I'm writing a Google Chrome extension, but I can't get chrome notifications to work.我正在编写一个 Google Chrome 扩展程序,但我无法让 Chrome 通知工作。
The goal (at least for testing purposes): to display a notification when the user clicks on the extension icon on the top right.目标(至少出于测试目的):当用户单击右上角的扩展图标时显示通知。
What's happening: clicking on the extension icon only displays the "Test body" text from popup.html
but no notifications.发生了什么:单击扩展图标仅显示popup.html
的“测试正文”文本,但不显示通知。 Inspecting the console for the extension, I can see the result of the callback function: Last error: undefined
.检查扩展的控制台,我可以看到回调函数的结果: Last error: undefined
。
EDIT: I've tried using Google's Notification API to test the functionality and it also seemed to not work.编辑:我尝试使用 Google 的 Notification API 来测试功能,但它似乎也不起作用。This link talks about a bug on the Chrome notifications, so I worry that might be the issue.此链接讨论了 Chrome 通知中的一个错误,所以我担心这可能是问题所在。 Any input about the bug would be appreciated.任何有关该错误的输入将不胜感激。
Here is my code:这是我的代码:
manifest.json:清单.json:
{
"name": "Test Extension",
"description": "Test description",
"version": "1.0",
"manifest_version": 2,
"icons": { "16": "images/icon16.png", "48": "images/icon48.png", "128": "images/icon128.png" },
"content_scripts": [
{
"matches": ["*://*.amazon.com/*buy*"],
"js": ["js/content.js"]
}
],
"browser_action": {
"default_popup": "html/popup.html",
"default_title": "Test Extension"
},
"background": {
"scripts": ["js/background.js"]
},
"permissions": [
"notifications"
],
"web_accessible_resources": ["images/icon48.png"]
}
popup.html:弹出窗口.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Nubank Credit Control</title>
</head>
<body>
<p>Test body</p>
<script src="../js/popup.js"></script>
</body>
</html>
content.js:内容.js:
chrome.runtime.sendMessage({
url: window.location.href
});
background.js:背景.js:
chrome.runtime.onMessage.addListener(function () {
const options = {
type: "basic",
iconUrl: chrome.extension.getURL("../images/icon48.png"),
title: "This is the title",
message: "This is the main message of the notification",
};
chrome.notifications.create("notifId", options, function() {console.log("Last error:", chrome.runtime.lastError);});
});
Actually, notifications can be triggered in popup.js as well.实际上,在 popup.js 中也可以触发通知。 As far as I can see the issue is in how you call chrome.notifications.create().据我所知,问题在于您如何调用 chrome.notifications.create()。 The first parameter is optional but if you still want to pass it, it should be a valid notification id, not just 'notifId'.第一个参数是可选的,但如果您仍然想传递它,它应该是一个有效的通知 ID,而不仅仅是“notifId”。
For testing purposes this should be enough:出于测试目的,这应该足够了:
manifest.json清单文件
{
"name": "Test Extension",
"description": "Test description",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
"default_popup": "html/popup.html",
"default_title": "Test Extension"
},
"permissions": [
"notifications"
]
}
popup.js弹出窗口.js
const options = {
type: "basic",
iconUrl: "../images/icon48.png",
title: "Popup.js",
message: "Hello from popup.js!"
};
chrome.notifications.create(options);
Or, in case of using background.js:或者,在使用 background.js 的情况下:
manifest.json清单文件
{
"name": "Test Extension",
"description": "Test description",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
"default_title": "Test Extension"
},
"background": {
"scripts": ["js/background.js"]
},
"permissions": [
"notifications"
]
}
background.js背景.js
const options = {
type: "basic",
iconUrl: "../images/icon48.png",
title: "Background.js",
message: "Hello from background.js!"
};
chrome.browserAction.onClicked.addListener(function (){
chrome.notifications.create(options);
});
chrome.notifications API 仅在后台上下文中可用,请参阅: https ://stackoverflow.com/a/6189524/3862289 以获得完整的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.