首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >移动(Chrome)浏览器通知

移动(Chrome)浏览器通知
EN

Stack Overflow用户
提问于 2016-05-22 13:49:07
回答 1查看 1.6K关注 0票数 0

我正在尝试将通知推送到移动浏览器。我跟踪了来自API的文档

它正在桌面上运行Chrome,但它不在移动设备上工作,尽管文档中说它应该这样做。我也尝试过这个例子https://jsbin.com/ziwod/2/edit?html,js,output,但它仍然不起作用。

下面您可以找到我的代码:

代码语言:javascript
复制
<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>

这在桌面上有效,但在移动平台上不起作用。谢谢您抽时间见我!

EN

回答 1

Stack Overflow用户

发布于 2022-07-22 11:49:01

现在,您只能从用户启动的事件中调用Notification.requestPermission,如单击此处:

代码语言:javascript
复制
<a href="#" onclick="Notification.requestPermission().then(r => alert(r))">Yes, allow notifications!</a>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37375196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档