Detect if Rich Notifications in Chrome are available

旧时模样 提交于 2019-12-22 04:47:14

问题


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.


回答1:


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.




回答2:


Just use this code:

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


来源:https://stackoverflow.com/questions/16774296/detect-if-rich-notifications-in-chrome-are-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!