7

My Chrome extension makes heavy use of webkitNotifications. I want to switch to the new rich notifications (chrome.notifications) but those aren't available on all platforms yet and at the moment of writing only in the beta channel and up. If the rich notifications aren't available webkitNotifications should be used as fallback. Thus I'm looking for the best solution to implement this:

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

I tried checking chrome.notifications.create for undefined but it's even defined for Chrome 27 with the rich notifications disabled in chrome://flags.

Rob W
  • 341,306
  • 83
  • 791
  • 678

2 Answers2

5

To detect if you have rich notifications, the most reliable way is currently to test for the existence of webkitNotifications.createHTMLNotification - if that function is undefined, then rich notifications have been switched on.

Girish
  • 4,692
  • 4
  • 35
  • 55
Somas T
  • 66
  • 1
  • 1
    Wouldn't it make more sense to check the inverse of this? To check they've been turned on, not that another feature isn't available? – Stuart.Sklinar May 28 '13 at 08:34
  • Thank you, checking `webkitNotifications.createHTMLNotification` for `undefined` works! I have confirmed it with Chromium 29 (dev) on Linux where the Rich Notifications are not available yet: `createHTMLNotification` is still defined. On Windows it is undefined starting from Chrome 28 (beta). – user2425107 May 28 '13 at 09:35
0

Just use this code:

if (webkitNotifications && webkitNotifications.createHTMLNotification) {
    //HTML notifications
} else if (chrome.notifications && chrome.notifications.create) {
    //Rich notifications
}
Alex L.
  • 56
  • 3
  • I doubt this is really needed anymore though. Rich notifications are enabled across all platforms for quite some time now. – Xan Nov 12 '14 at 09:30