2013-05-27 25 views
7

我的Chrome擴展程序大量使用webkitNotifications。我想切換到new rich notifications (chrome.notifications),但那些目前尚未在所有平臺上提供,並且只能在測試版頻道和更高版本中編寫。如果豐富的通知不可用,則應使用webkitNotifications作爲回退。因此,我在尋找實現這一目標的最佳解決方案:檢測Chrome中的Rich通知是否可用

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

我試圖檢查chrome.notifications.create未定義的,但它甚至爲Chrome 27在chrome://flags禁用豐富的通知定義。

回答

5

要檢測,如果你有rich notifications,最可靠的方法目前是測試的webkitNotifications.createHTMLNotification存在 - 如果該功能是不確定的,那麼rich notifications已經switched on

+1

那豈不是更有意義的檢查此相反?要檢查它們是否已打開,不是沒有其他功能不可用? –

+0

謝謝,檢查'webkitNotifications.createHTMLNotification' for undefined'工程!我已在Linux上的Chromium 29(dev)上確認了它,但Rich Notifications尚未提供:'createHTMLNotification'仍然定義。在Windows上,它從Chrome 28(測試版)開始未定義。 – user2425107

0

只需使用此代碼:

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

我懷疑這是真的需要了。現在,在所有平臺上都可以使用豐富的通知。 – Xan