如何在每个chrome通知中创建点击监听器?

时间:2017-08-25 19:34:34

标签: javascript google-chrome-extension notifications

我想创建多个chrome-extension-notifications侦听器,每个侦听器都有一个单击侦听器。但我无法按照自己的方式行事。

当我点击一个通知时,所有通知监听器都被播放。这是 basic_notification.js

function notifyMe() {
    var notificationId = 'notId1';
    var notificationId2 = 'notId2';
    var notificationId3 = 'notId3';

    var opt = {
        type: 'basic',
        iconUrl: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
        title: 'anyTitle',
        message: 'anyString'
    };
    chrome.notifications.create(notificationId, opt  , function(notificationId) {
        console.log(notificationId);
    });
    chrome.notifications.create(notificationId2, opt  , function(notificationId2) {
        console.log(notificationId2);
    });
    chrome.notifications.create(notificationId3, opt  , function(notificationId3) {
        console.log(notificationId3);
    });
}

此代码可以一次创建3个通知。 我将在每个chrome-extension-notifications中编写监听器。

chrome.notifications.create(notificationId, opt  , function(notificationId) {
            chrome.notifications.onClicked.addListener(notificationClicked);
            console.log(notificationId);
        });

function notificationClicked() {
    console.log("The notification1 was clicked");
}

这是所有代码: notification.js

function notifyMe() {
    var notificationId = 'notId1';
    var notificationId2 = 'notId2';
    var notificationId3 = 'notId3';

    var opt = {
        type: 'basic',
        iconUrl: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
        title: 'anyTitle',
        message: 'anyString'
    };
    chrome.notifications.create(notificationId, opt  , function(notificationId) {
        chrome.notifications.onClicked.addListener(notificationClicked);
        console.log(notificationId);
    });
    chrome.notifications.create(notificationId2, opt  , function(notificationId2) {
        chrome.notifications.onClicked.addListener(notificationClicked2);
        console.log(notificationId2);
    });
    chrome.notifications.create(notificationId3, opt  , function(notificationId3) {
        chrome.notifications.onClicked.addListener(notificationClicked3);
        console.log(notificationId3);
    });
}
function notificationClicked() {
    console.log("The not1 was clicked");
}
function notificationClicked2() {
    console.log("The not2 was clicked");
}
function notificationClicked3() {
    console.log("The not3 was clicked");
}

当我按下一个时,我想只有一个。我该怎么办?

0 个答案:

没有答案