7

做完某事后,我运行以下代码:

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

4

5 回答 5

16

您可以使用notification.cancel();

于 2011-05-13T14:53:03.300 回答
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 秒后自动关闭。

于 2012-08-19T20:01:00.310 回答
3

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

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

于 2011-04-20T14:40:17.467 回答
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);
}

}

好的,当我创建通知时,请记住 idnotificationId和 settimeout 清除此 id

于 2015-12-10T07:23:58.717 回答
-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);
        });
     }

   }
于 2017-02-11T19:44:54.187 回答