0

I'm trying to push notifications to a mobile browser. I followed the documentation from https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API

It's working on Chrome on desktop, but it is not working on mobile, even though the documentation says it should. I also tried this example https://jsbin.com/ziwod/2/edit?html,js,output as well but it still does not work.

Below you can find my code:

<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>

This works on desktop, but not on mobile. Thank you for your time!

Irene Texas
  • 1,731
  • 2
  • 12
  • 8
  • Just checked out on "Can I Use". Seems that Notifications aren't supported (yet) on Chrome Mobile. Here's the link http://caniuse.com/#feat=notifications – Pierfrancesco May 22 '16 at 14:51
  • Thank you, but that's quite odd, if you check https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API you can see that there are screenshots of how it should work. However, I am going to try it with Firefox on mobile. – Irene Texas May 23 '16 at 06:49
  • tested with few chrome mobile browsers and some natives ones. In Chrome none of my attempts has worked. With Stock browser (just 1 of 2 tested has worked fine) I got the notification. The screenshot you were talking about I think is from an attempt made with a stock browser. Recap: Chrome - Nexus 6 (no) | Samsung S6 (no) Stock - Samsung S6 (yes) | LG g3 (no) Hope this can help – Pierfrancesco May 23 '16 at 12:38

1 Answers1

0

Today anyway you can only call Notification.requestPermission from a user-initiated event, like click on this here:

<a href="#" onclick="Notification.requestPermission().then(r => alert(r))">Yes, allow notifications!</a>
Gunther Schadow
  • 1,490
  • 13
  • 22