【问题标题】:Close/clear a chrome extension notification while notification panel is open在通知面板打开时关闭/清除 chrome 扩展通知
【发布时间】:2014-05-29 10:49:51
【问题描述】:

参考:https://developer.chrome.com/apps/notifications

我正在使用 chrome.notifications.create(string id, object options, function callback);创建 chrome 通知。

var id = 'list';

var options = {};
options.title = 'test';
options.iconUrl = 'notification_icon.png';
options.type = 'list';
options.message = "test";
options.buttons = [{title: 'test'}];
options.items = [{title: 'test', message:'test'}];

var createCallback = function(notificationId) { console.log(notificationId); };

chrome.notifications.create(id, options, createCallback); // returns 'list';

这会按预期创建一个通知。一切正常。

然后我调用 chrome.notification.clear(string id, function callback);

var id = 'list';

var clearCallback= function(wasCleared) { console.log(wasCleared); };

chrome.notification.clear(id, clearCallback); // returns true;

这会清除通知。一切正常。

EXCEPT如果通知面板打开,它不会清除通知。这在 99% 的情况下都不是大问题。直到我在通知中实现了按钮代码。

使用 chrome.notifications.onButtonClicked.addListener(函数回调);单击时,我正在调用清除通知面板代码,并在清除后报告。

var onButtonClickedCallback = function (notificationId, buttonIndex) {
    console.log(notificationId, buttonIndex);
    if ( notificationId == 'list' ) {
        chrome.notification.clear(id, clearCallback); // returns true;
    }
}
chrome.notifications.onButtonClicked.addListener(onButtonClickedCallback); // onClick it returns 'list', 0

但我正在看它。一旦通知面板关闭并再次打开,我可以确认它实际上已经消失了。但显然,由于我单击通知上的按钮,面板是打开的,但它并没有像我希望的那样清除。

所有这一切都在没有持久性的扩展背景中运行:假属性(所以脚本总是被加载,因为我可以看到输出,我知道函数正在被调用)。

我是否忽略了什么?我没有看到任何处理关闭通知面板的功能。据我所知,我正在清除通知,但面板没有更新它的显示。

我在 Win8 上使用 Chrome 37.0.2019.0 canary

如果有人能提出我可能错过的建议,我将不胜感激。我的谷歌搜索显示人们对 HTML 通知有问题。

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension notifications


    【解决方案1】:

    这是一个known bug,或者更确切地说是一个旧的设计决策,进展甚微。

    为问题加注星标以提高其优先级。我也有同样的痛苦。

    【讨论】:

    • 谢谢,不知道为什么我的google-fu找不到这个。
    【解决方案2】:

    这是我几个月来一直在使用的解决方法:

    // open a window to take focus away from notification and there it will close automatically
    function openTemporaryWindowToRemoveFocus() {
       var win = window.open("about:blank", "emptyWindow", "width=1, height=1, top=-500, left=-500");
       win.close();
    }
    
    chrome.notifications.clear("", function(wasCleared) {
        openTemporaryWindowToRemoveFocus()
    });
    

    【讨论】:

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