Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if browser notification is available

I am working with browser notifications and because it is not available for every browser I want to check in my JS-code, if it is available.

I took a look in the Mozilla developer section:
https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API

They tell me that I should use this code to check whether a browser has notification support or not:

  if (!("Notification" in window)) {
    alert("This browser does not support system notifications");
  }

I copied that code into my website; the notification still works in the browsers where it worked before but it blocks the execution of other code (as it did before the 'check'-code).

For example: in the edge browser, I get the console error:

 'Notification is undefined'

So what is the preferred method from you to check whether a browser has notification capability?

like image 709
binaryBigInt Avatar asked Jul 17 '16 14:07

binaryBigInt


People also ask

Do browser notifications work on mobile?

Web push notifications are delivered on a user's desktop or mobile screen any time their browser is open – whether or not the user is on the website.


1 Answers

The following code would check whether the browser is compatible with the web notification API

if ('Notification' in window) {
  // API supported
} else {
  // API not supported
}
like image 135
Michael Dadi Avatar answered Oct 23 '22 21:10

Michael Dadi