我正在尝试将通知推送到移动浏览器。我跟踪了来自API的文档
它正在桌面上运行Chrome,但它不在移动设备上工作,尽管文档中说它应该这样做。我也尝试过这个例子https://jsbin.com/ziwod/2/edit?html,js,output,但它仍然不起作用。
下面您可以找到我的代码:
<script type="text/javascript">
createNotification("Reminder", "TEST", [100, 0, 200])
function createNotification(text_before, title, vibration) {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support system notifications");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification(text_before, { body: title, vibrate: vibration });
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification(text_before, { body: title, vibrate: vibration });
}
});
}
}
</script>这在桌面上有效,但在移动平台上不起作用。谢谢您抽时间见我!
发布于 2022-07-22 11:49:01
现在,您只能从用户启动的事件中调用Notification.requestPermission,如单击此处:
<a href="#" onclick="Notification.requestPermission().then(r => alert(r))">Yes, allow notifications!</a>https://stackoverflow.com/questions/37375196
复制相似问题