chrome.notifications.on已关闭,无法在background.js中使用Google chrome扩展程序

时间:2019-06-21 23:46:27

标签: javascript google-chrome-extension notifications

我正在构建一个非常基本的google chrome扩展程序,该扩展程序必须侦听来自google chrome的所有收到的通知!

我的扩展程序的文件结构;

  • manifest.json
  • background.js
  • 图标
    • main.png

manifest.json

    {
    "manifest_version": 2,
    "name": "My Extension",
    "version": "1.0.0",
    "browser_action": {
        "default_icon": {                   
          "16": "icons/main.png",          
          "24": "icons/main.png",           
          "32": "icons/main.png"            
        }
    },
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "permissions": [
        "notifications"
      ]
}

background.js

console.log('Lady Gaga');

chrome.browserAction.onClicked.addListener(function(){
    console.log('Clicked');
});

chrome.notifications.onClosed.addListener(function (s,b){
    console.log('notification Closed');
});

chrome.notifications.onClicked.addListener(function (){
    console.log('notification Clicked');
});

chrome.notifications.onButtonClicked.addListener(function (){
    console.log('notification Button Clicked');
});

因此,如果我点击扩展程序的按钮

chrome.browserAction.onClicked

被解雇,当我检查后台日志时,我看到“ Lady Gaga”和“ Clicked”日志被解雇了!

接下来,我正在尝试查看通知侦听器的日志。为了激发通知的听众,他们发现了一个网站,该网站具有可轻松发送通知的基本HTML按钮。

https://web-push-book.gauntface.com/demos/notification-examples/

我正在使用此网站自行发送通知,以测试定义为通知事件的侦听器的后台事件侦听器。

但是所有通知事件均未触发,无法正常工作。我看不到“单击通知按钮”或“已关闭通知”之类的日志

那种情况怎么了?

1 个答案:

答案 0 :(得分:0)

manifest.json

{
    "manifest_version" : 2,
    "name" : "demo1",
    "description" : "Demo",
    "version" : "0.1",
    "background" : { 
        "scripts" : ["background.js"],
        "persistent" : false
    },
    "permissions" : [
        "notifications"
    ]
}

background.js

var description = "dvcvv";
var news = "cvcvcvcvcvcvcvcvcv"; 
var pattern = {
    OPTION : "list",    
};

var a1 = {
    title : "xxxxxxxxxxxxxxxxxx",
    message : "dsdsdsdX"
};
var a2 = {
    title : "yyyyyyyyyyyy",
    message : "sdsdsdsdY"
};
var butt1 = {
    title : "Button 1 "
};
var butt2 = {
    title : "Button 2"
};

var notifopt = {
    type : pattern.OPTION,
    iconUrl : "icon.png",
    title : description,
    message : news,
    buttons : [butt1,butt2],
    items : [a1,a2]
};


chrome.notifications.create(notifopt,function() {
    console.log('Lady Gaga');
});

chrome.notifications.onClicked.addListener(function() { 
    console.log('notification Clicked');

});
chrome.notifications.onClosed.addListener(function() {
    console.log('notification Closed');
});
chrome.notifications.onButtonClicked.addListener(function() {
    console.log('notification Button Clicked');
});

google chrome->更多工具->扩展程序->拆包加载->刷新扩展程序->显示通知->单击button1->单击在后台查看视图页面- ->您会看到console.log