2011-04-20 78 views
7

做的事情后,我运行此代码:Chrome扩展的发展:自动关闭通知框

var notification = webkitNotifications.createNotification(
    'icon.png', // icon url - can be relative 
    'Done!', // notification title 
    'Just updated your list!' // notification body text 
    ); 
    notification.show(); 

这当然会弹出一个通知到用户屏幕。

无论如何要这个通知的时间,以便自动关闭在X秒的时间?

谢谢! R

回答

16

您可以使用notification.cancel();

+0

我喜欢'notification.cancel();',因为它可以从创建通知的相同函数中调用。 – smfoote 2011-06-28 17:13:46

+0

谢谢!完全像我想要的一样! – Ryan 2011-07-03 16:35:36

+0

可能有API的更新,现在应该使用notification.close()。 – William 2015-08-28 01:55:00

3

您可以在通知的HTML页面中调用window.close()。这将关闭通知。

要在特定时间关闭,调用类似setTimeout(function() { window.close(); }, timeInMicroseconds);应该是有效的。

+0

谢谢你!那点击! – Ryan 2011-04-21 16:07:12

9
var notification = webkitNotifications.createNotification('images/icon-48x48.png',"This is  Title","Biswarup Adhikari Notification"); 
notification.show(); 
setTimeout(function(){ 
notification.cancel(); 
},2000); 

Chrome的通知将在2000年后毫秒或2秒自动关闭。

0
function show(title, message, icon) { 
try { 
    icon = icon || 'src/img/icons/icon48.png'; 
    var self = this; 
    var isClosed = false; 
    var notificationId = "posting_" + Math.random(); 

    chrome.notifications.create(notificationId, { 
     type: "basic", 
     title: title + "!", 
     message: message, 
     iconUrl: icon 
    }, function (nId) { 
    }); 

    setTimeout(function() { 
     if (!isClosed) 
      chrome.notifications.clear(notificationId, function (wasCleared) { 
      }); 
    }, 3000); 
} catch (e) { 
    alert(e.message); 
} 

}

+0

也请解释你改变了什么,为什么 – blckbird 2015-12-10 07:42:54

-1
//Use requireInternaction and set it to true for notification to not to auto-hide. 

function showNotification() { 
    var options = { 
     body: 'The Subtitles will Go Here', 
     requireInteraction: true 
    }; 

    if (window.Notification && Notification.permission !== "denied") { 
     Notification.requestPermission(function (status) { // status is "granted", if accepted by user 

var n = new Notification('Title', options); 
     }); 
    } 

    }