2
我在我的漸進式網絡應用程序中爲服務人員使用新的Web推送API,但擔心用戶在最初啓用後禁用了推送通知許可。如何衡量從我的漸進式網絡應用中禁用推送通知的人數?
檢測禁用通知權限的用戶(即添加儀器/分析)的最佳方式是什麼,以便我可以跟蹤此問題?
我在我的漸進式網絡應用程序中爲服務人員使用新的Web推送API,但擔心用戶在最初啓用後禁用了推送通知許可。如何衡量從我的漸進式網絡應用中禁用推送通知的人數?
檢測禁用通知權限的用戶(即添加儀器/分析)的最佳方式是什麼,以便我可以跟蹤此問題?
我相信你所看到的是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。
如果他們在我的網站未打開時撤銷通知本身上的「網站設置」按鈕的權限會怎麼樣?我不會錯過這個活動嗎?當頁面打開時,我需要輪詢嗎? – owencm
您可以使用由'navigator.permissions.query'返回的初始'notificationPerm.state'值來處理該用例。我已經調整了代碼示例中的註釋來澄清。一種方法可能是將最初的'notificationPerm.state'值與先前已知的值進行比較,該值可以保存在例如'localStorage'中。 –