【问题标题】:Detect if Rich Notifications in Chrome are available检测 Chrome 中的丰富通知是否可用
【发布时间】:2013-05-22 09:01:57
【问题描述】:

我的 Chrome 扩展程序大量使用了 webkitNotifications。我想切换到new rich notifications (chrome.notifications),但这些还不是在所有平台上都可用,目前仅在 beta 通道及更高版本中编写。如果丰富的通知不可用,则应使用 webkitNotifications 作为后备。因此,我正在寻找实现这一点的最佳解决方案:

if(richNotificationsAvailable())
   chrome.notifications.create(...);
else
   webkitNotifications.createNotification(...).show();

我尝试检查 chrome.notifications.create 是否未定义,但它甚至是为 Chrome 27 定义的,在 chrome://flags 中禁用了丰富的通知。

【问题讨论】:

    标签: google-chrome-extension rich-notifications


    【解决方案1】:

    只需使用此代码:

    if (webkitNotifications && webkitNotifications.createHTMLNotification) {
        //HTML notifications
    } else if (chrome.notifications && chrome.notifications.create) {
        //Rich notifications
    }
    

    【讨论】:

    • 我怀疑这是否真的需要了。很长一段时间以来,所有平台都启用了丰富的通知。
    【解决方案2】:

    要检测你是否有rich notifications,目前最可靠的方法是测试webkitNotifications.createHTMLNotification是否存在——如果该函数未定义,那么rich notifications已经是switched on

    【讨论】:

    • 检查它的倒数不是更有意义吗?要检查它们是否已打开,而不是其他功能不可用?
    • 谢谢你,检查webkitNotifications.createHTMLNotification undefined 有效!我已经在 Linux 上使用 Chromium 29 (dev) 确认了这一点,其中 Rich Notifications 尚不可用:createHTMLNotification 仍被定义。在 Windows 上,从 Chrome 28(测试版)开始未定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多