回答

3

我相信你所看到的是Permissions API,其中包括change事件。如果用戶稍後改變主意,那麼該事件就會被初次用戶決定觸發。

在其最基本的,你可以這樣做:

if ('permissions' in navigator) { 
    navigator.permissions.query({name:'notifications'}).then(function(notificationPerm) { 
    // notificationPerm.state is one of 'granted', 'denied', or 'prompt'. 
    // At this point you can compare notificationPerm.state to a previously 
    // cached value, and also listen for changes while the page is open via 
    // the onchange handler. 

    notificationPerm.onchange = function() { 
     // Permissions have changed while the page is open. 
     // Do something based on the current notificationPerm.state value. 
    }; 
    }); 
} 

有幾個很好的資源,在那裏演示如何可以這樣使用:

從版本43開始,Chrome支持Permissions API,在即將發佈的版本45版本中支持Firefox。

+0

如果他們在我的網站未打開時撤銷通知本身上的「網站設置」按鈕的權限會怎麼樣?我不會錯過這個活動嗎?當頁面打開時,我需要輪詢嗎? – owencm

+0

您可以使用由'navigator.permissions.query'返回的初始'notificationPerm.state'值來處理該用例。我已經調整了代碼示例中的註釋來澄清。一種方法可能是將最初的'notificationPerm.state'值與先前已知的值進行比較,該值可以保存在例如'localStorage'中。 –

相關問題