我有一个模式,首先弹出询问用户是否想要接收特别优惠,如果他们点击是,然后我拉入推送通知的代码,以便他们可以允许通知。如果他们已经允许通知,我不希望弹出模式。我正在寻找一种方法来检查通知是否已经被用户允许,使用谷歌铬。
发布于 2016-06-23 00:14:49
检查通知对象的permission属性:
if (Notification.permission !== "granted") {
// ask for permission发布于 2016-06-24 19:38:04
除了Denys Séguret回答的Notification.permission之外,还有较新的、不太受支持但更通用的Permissions API.
这里有一个基于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();https://stackoverflow.com/questions/37973304
复制相似问题