admin管理员组

文章数量:1335160

There is a basic example that shows how Notifications Web API works.

The example works well in Windows and Linux with Firefox/Chrome web browsers.

However, the example doesn't show any notifications in Android with Chrome/Brave web browsers.

I cannot find any information in Web API documentation of how to use Notifications in Android. As well, there is no any direct information that Notifications API doesn't work in Android.

Does Android supports the Web API Notifications?

URL of the example

Code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Notifications example</title>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <!-- Bootstrap/JQuery. -->
        <link rel="stylesheet" href="/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous" />
        <script src="/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
        <script src="/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
        <script src="/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous"></script>
    </head>
    <body class="container mt-3">
        <h2 class="mt-5">Notifications example</h2>

        <p>This example use only JavaScript. No server side used.</p>

        <input type="button" id="subscribe" value="Subscribe" class="btn btn-success" /><br />

        <button onclick="notifyMe()" class="btn btn-info mt-3">Notify me!</button>

        <button onclick="notifyMeWith7SecDelay()" class="btn btn-info mt-3">Notify me with 7 sec delay!</button>

        <script>
            $("#subscribe").on("click", function () {
                let promise = Notification.requestPermission();
            });

            function notifyMe() {
                if (!("Notification" in window)) {
                    alert("This browser does not support desktop notification");
                } else if (Notification.permission === "granted") {
                    const notification = new Notification("Hi there!", {
                        body: "Is an example of a notification"
                    });
                } else if (Notification.permission !== "denied") {
                    Notification.requestPermission().then((permission) => {
                        if (permission === "granted") {
                            const notification = new Notification("Hi there!", {
                                body: "Is an example of a notification"
                            });
                        }
                    });
                }
            }

            function notifyMeWith7SecDelay() {
                setTimeout(notifyMe, 7000);
            }
        </script>
    </body>
</html>

本文标签: javascriptHow to use Notifications Web APIStack Overflow