Microsoft Edge notification in an extension

馋奶兔 提交于 2019-12-25 08:17:05

问题


Can I use notifications in an Edge extension?

I'm converting a chrome extension to Edge and this extension uses notifications but it's crashing in Edge. After some research I found Edge notifications are different from chrome. So I created a test html page and ran it on IIS localhost.

<!DOCTYPE html>

<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>Test Notifications</title>

</head>
<body>
<script>
        if ( "Notification" in window ) {
            let ask = Notification.requestPermission();

            ask.then( permissions => {
                if (permissions === "granted") {
                let message = new Notification ("Title", { body: "Hello Nurse", icon: "icons/icon.png" });

                }
            });
        }
</script>

    <h2>Test Notifications</h2>
    <p>Please turn on notifications</p>

</body>
</html>

Notifications worked! Now I tried plugging this into the extension and I don't get the permission window. Do you know why that's not coming up? Sometimes I get a message saying notifications is disabled for unknown sources. I click the button that says "turn on anyways" but notification still doesn't show up.

Are notifications blocked on all extensions or is it because I loaded this from my machine instead of the store? When this gets delivered to the store, will the notifications work?


Edit, Additional Information

I always forget something when writing these questions.

Manifest.json

{
"manifest_version": 2,
"name": "__MSG_extension_name__",
"version": "XXXXX",
"description": "__MSG_extension_description__",
"author": "XXXXX",
"icons": {
    "16": "icons/icon16.png",
    "32": "icons/icon32.png",
    "48": "icons/icon48.png",
    "64": "icons/icon64.png",
    "128": "icons/icon.png"
},
"default_locale": "en_US",
"browser_action": {
    "default_icon": {
        "16": "icons/icon16.png",
        "32": "icons/icon32.png",
        "48": "icons/icon48.png",
        "64": "icons/icon64.png",
        "128": "icons/icon.png"
    },
    "default_title": "__MSG_extension_name__",
    "default_popup": "popup.html"
},
"background": {
    "page": "background.html",
    "persistent": true
}, 
"homepage_url": "http://www.xxxxx.com/",
"inprivate": "spanning",
"minimum_edge_version": "33.14281.1000.0",
"offline_enabled": true,
"permissions": [
    "<all_urls>",
    "notifications",
    "tabs",
    "webRequest",
    "webRequestBlocking",
    "webNavigation",
    "storage",
    "unlimitedStorage",
    "contextMenus"
],
"content_scripts": [
    {
        "css": [
            "css/script.css",
            "lib/jQuery-ui/jquery-ui.css"
        ],
        "js": [
            "lib/jQuery/jquery.min.js",
            "lib/jQuery-ui/jquery-ui.min.js",
            "js/script.min.js",
            "js/xxxxx.JSInterface.Types.min.js",
            "js/xxxxx.JSInterface.Utility.min.js",
            "js/xxxxx.JSInterface.API.min.js",
            "content_script.min.js"
        ],
        "matches": [
            "http://*/*",
            "https://*/*"
        ],
        "run_at": "document_end",
        "all_frames": true
    }
],
"web_accessible_resources": [
    "css/script.css",
    "icons/*.png",
    "img/*.png",
    "js/script.min.js",
    "js/xxxxx.JSInterface.Types.min.js",
    "js/xxxxx.JSInterface.Utility.min.js",
    "js/xxxxx.JSInterface.API.min.js",
    "lib/jQuery/jquery.min.js",
    "lib/jQuery-ui/jquery-ui.min.js",
    "lib/jQuery-ui/images/*.png",
    "lib/Fancytree/dist/skin-win8/ui.fancytree.css",
    "lib/Fancytree/dist/skin-win8/icons.gif",
    "lib/Fancytree/dist/skin-lion/ui.fancytree.css",
    "lib/Fancytree/dist/skin-lion/icons.gif",
    "lib/DataTables/media/images/*.png"
],
"-ms-preload": {
    "backgroundScript": "backgroundScriptsAPIBridge.js",
    "contentScript": "contentScriptsAPIBridge.js"
}
}

I tried to edit out all my company information, if I forgot any please let me know.

To summarize this I have a background page that loads 5 javaScript files. Also have a pop up html file that pops up to as for user name and password. This file has it's own javaScript file that checks the user name and password and notifies the results.


回答1:


You can use the same chrome notification code on Edge.

var options = {
  "type": "basic",
  "title": "Test extension",
  "message": "Test",
  "iconUrl": "Logo.png",
  "requireInteraction": false
};
chrome.notifications.create("id1", options);

I've done it and it works. The API bridges you have on your manifest file take care of it.

However, I had trouble getting the notifications to show. The problem was the length of the "name" attribute on the manifest. It was too long. With a length of 41 characters it didn't show notifications, despite showing no error on F12 tools console. I shortened it to 21 characters and notifications started showing up. When I changed the name I had to remove the extension from Edge and then reinstall it. Simply refreshing the extension wasn't enough.



来源:https://stackoverflow.com/questions/41029626/microsoft-edge-notification-in-an-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!