【问题标题】:Chrome extension development: auto close the notification boxChrome扩展开发:自动关闭通知框
【发布时间】:2011-08-09 13:49:26
【问题描述】:

做完某事后,我运行这段代码:

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

当然会在用户屏幕中弹出通知。

无论如何都要为这个通知计时,以便它在 X 秒内自动关闭?

谢谢! 回复

【问题讨论】:

标签: google-chrome notifications auto-close


【解决方案1】:

您可以使用notification.cancel();

【讨论】:

  • 我喜欢notification.cancel();,因为它可以从创建通知的同一个函数中调用。
  • 谢谢!完全按照我的意愿工作!
  • API 可能有更新,您现在应该使用 notification.close()。
【解决方案2】:
var notification = webkitNotifications.createNotification('images/icon-48x48.png',"This is       Title","Biswarup Adhikari Notification");
notification.show();
setTimeout(function(){
notification.cancel();
},2000);

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 谢谢!恰到好处!
    【解决方案4】:
    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

    【讨论】:

    • 请同时解释您更改的内容和原因
    【解决方案5】:
    //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);
            });
         }
    
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 2011-06-26
      • 2012-07-18
      相关资源
      最近更新 更多