问题
I have a simple call to the chrome.notifications.create(id,options) function and I have checked the arguments ten times already.
The id argument is a string. The options argument is an object like this:
{type:"basic",message:"message",title:"message",iconUrl:chrome.extension.getURL("icons/icon.png")}
It is only working in Firefox though. Edge Opera and Chrome all fail to display the notification. Edge just crashes!
I've checked everything. There are no errors and the iconUrl is correct.
Also I checked the name field of the manifest json. It's fine. (Microsoft Edge notification in an extension)
Sample relevant and simplified code
This simplified version has the same problem as the complete one.
//For hooking up event handlers
try {
chrome.runtime.onStartup.addListener(doHandshake);
}catch(ex) {
console.error("onStartup function in Edge is not supported.");
}
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequestCallback, { types:['main_frame'],urls:['*://*/*']}, ['blocking']);
function onBeforeRequestCallback(requestDetails) {
showMessage();
return {};
}
function showMessage() {
console.log("about to show notification...");
var notificationOptions ={type:"basic",message:"msg",title:"title",iconUrl:chrome.extension.getURL("icons/icon.png")};
chrome.notifications.create("",notificationOptions);
}
The manifest.json
{
"manifest_version": 2,
"author" : "whatever Ltd.",
"name": "21charsName",
"version": "1.0",
"description": "whatever",
"permissions": [
"*://*/*",
"tabs",
"webRequest",
"webRequestBlocking",
"storage",
"notifications"
],
"browser_action": {
"default_icon": "icons/icon.png",
"default_title": "extension"
},
"background": {
"scripts": [
"background.js"
],
"persistent": true
},
"web_accessible_resources": []
}
回答1:
It turned out that it's a bug in chromium-based browsers. It seems once you disable notifications for an extension it never ever show s notifications again because there's no way to re-enable it, and you'll have to reinstall the browser, though my attempt at that yielded nothing either.
I had to try on another machine with a brand new Chrome installation and the notifications started showing on that machine.
As for Edge, it turned out to be a bug too. :-/
回答2:
If you are using Mac and Chrome 59+, it might be because the MacOS native notification is disabled for Chrome. Here's two possible solutions:
Solution 1
Open Chrome > Go chrome://flags
> Search Enable native notifications
> Change it to Disabled
> Relaunch Chrome
Solution 2
Go to MacOS System Preferences > Notifications > Turn on Notifications for Banners/Alerts as shown here (likely previously it's Off)
Reference 1
Reference 2
来源:https://stackoverflow.com/questions/55001031/chrome-web-extension-not-showing-notification-even-though-it-is-working-fine-in