2011-04-20 88 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); 
     }); 
    } 

    }