Chrome扩展程序开发:自动关闭通知框

时间:2011-04-20 14:30:39

标签: google-chrome notifications auto-close

做完之后我运行了这段代码:

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

5 个答案:

答案 0 :(得分:16)

您可以使用notification.cancel();

答案 1 :(得分: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秒后自动关闭。

答案 2 :(得分:3)

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

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

答案 3 :(得分: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);
}

}

好的,当我创建通知记住ID notificationId并且settimeout清除此ID

答案 4 :(得分:-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);
        });
     }

   }