检测是否在Chrome丰富的通知可用



我的Chrome扩展大量使用webkitNotifications。我想切换到新的丰富的通知(chrome.notifications),但这些并不是在所有平台上都可用,而且目前只在测试频道和更高的版本中写作。如果富通知不可用,webkitNotifications应该用作备用。因此,我正在寻找实现这一点的最佳解决方案:

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

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

要检测您是否有rich notifications,目前最可靠的方法是测试webkitNotifications.createHTMLNotification的存在-如果该函数未定义,则rich notifications已为switched on

使用下面的代码:

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

最新更新