Javascript Google Chrome扩展在用户单击后关闭通知

Javascript Google Chrome扩展在用户单击后关闭通知,javascript,google-chrome-extension,Javascript,Google Chrome Extension,Chrome扩展工作正常。我的问题是通知在7秒内关闭。我希望用户单击以关闭通知 function engine(){ var latestId; var ids; var messages = []; var newmessage = []; $.get('http://localhost/site/json.php',function (data){ var json = $.parseJSON(data); messa

Chrome扩展工作正常。我的问题是通知在7秒内关闭。我希望用户单击以关闭通知

function engine(){
    var latestId;
    var ids;
    var messages = [];
    var newmessage = [];

    $.get('http://localhost/site/json.php',function (data){
        var json = $.parseJSON(data);
        messages = json;
        ids = json[0].id;
        if(latestId == ids){
            //no update
        }else if(latestId === undefined){
            var run = {
                type: "basic",
                title: "Title",
                message: "Some message",
                iconUrl : "icon.png",
            };

            chrome.notifications.create(run);
        }    
    });
}

您可以使用
notification.close()

演示:

//请求页面加载权限
document.addEventListener('DOMContentLoaded',函数(){
if(Notification.permission!=“已授予”)
Notification.requestPermission();
});
函数notifyMe(){
如果(!通知){
警报('您的浏览器中没有桌面通知。请尝试使用Chromium');
返回;
}
if(Notification.permission!=“已授予”)
Notification.requestPermission();
否则{
var通知=新通知(“通知标题”{
图标:'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
身体:“嘿!已经通知你了!x”,
});
notification.onclick=函数(){
窗口打开(“http://stackoverflow.com/a/13328397/1269037");      
};
setTimeout(函数(){
通知。关闭();
}, 2000);
}
}

通知我!

此功能目前仅在beta频道中实现,而不在最新版本的chrome中实现。有关详细信息,请参阅

实现后,您将能够使用
requireInteraction:True
如:

var run = {
    type: "basic",
    title: "Title",
    message: "Some message",
    iconUrl : "icon.png",
    requireInteraction : True,
}

要实现此功能。

首先创建通知,并为其提供notificationID参数,以便稍后关闭

var notification = {
    type:"basic",
    title:"News From Us!",
    message:"Google Chrome Updated to v50!",
    iconUrl:"assets/images/icon.png"
};
chrome.notifications.create("notfyId",notification);
单击通知后,您可以使用其id(即“notfyId”)关闭通知

现在,当您单击通知时,它将关闭。

通知。关闭()用于关闭任何通知

function engine(){
    var latestId;
    var ids;
    var messages = [];
    var newmessage = [];

    $.get('http://localhost/site/json.php',function (data){
        var json = $.parseJSON(data);
        messages = json;
        ids = json[0].id;
        if(latestId == ids){
            //no update
        }else if(latestId === undefined){
            var run = {
                type: "basic",
                title: "Title",
                message: "Some message",
                iconUrl : "icon.png",
            };

            chrome.notifications.create(run);
        }    
    });
}
有关更多信息,请参阅下面的代码-

创建通知:

var notification = new Notification('OnlineOfferPrice', {
    icon: 'icon.png',
    body: "Test Message",
});
如果要对通知执行操作,请单击下面的代码。在完成业务逻辑后,它将自动关闭-

notification.onclick = function () { 
    //write your business logic here.
    notification.close();
};
如果未单击通知,并且您希望在6秒后自动关闭通知-

setTimeout(function() { notification.close(); }, 6000);

不确定你在问什么。您是希望在每次单击通知时将其关闭,还是希望在7秒后使其不隐藏并仅在用户交互时关闭?notification.cancel不是写入控制台的函数?您能在这里编写示例代码吗?@Arokiyanathan:​​​​​​​​​​​​​​​请给出您的答案,并将链接放入其中。另外,请尽量使你的答案详细和清楚。请参阅:window.location不起作用,但是window.open不感谢提示!
setTimeout(function() { notification.close(); }, 6000);