How to check if notifications is already allowed by user in google chrome? How to check if notifications is already allowed by user in google chrome? google-chrome google-chrome

How to check if notifications is already allowed by user in google chrome?


Check the permission property of the Notification object:

if (Notification.permission !== "granted") {    // ask for permission


Alongside Notification.permission as answered by Denys Séguret there is the newer, less well-supported but more general Permissions API.

Here's a quick usage example, based on the one from MDN:

function handlePermission() {    return navigator.permissions            .query({name:'notifications'})            .then(permissionQuery)            .catch(permissionError);}function permissionQuery(result) {    console.debug({result});    var newPrompt;    if (result.state == 'granted') {        // notifications allowed, go wild    } else if (result.state == 'prompt') {        // we can ask the user        newPrompt = Notification.requestPermission();    } else if (result.state == 'denied') {        // notifications were disabled    }    result.onchange = () => console.debug({updatedPermission: result});    return newPrompt || result;}////handlePermission();