Pschmeltz
Pschmeltz

Reputation: 150

Vaadin Desktop Notifications

Is there any support for html5 browser 'desktop notifications' in Vaadin? I've looked for this and can't find anything specific.

I've tried something like this with no luck.

        JavaScript.getCurrent().execute(
            "if (window.webkitNotifications) {" +
                    "if (window.webkitNotifications.checkPermission() == 0) { // 0 is PERMISSION_ALLOWED" +
            "    window.webkitNotifications.createNotification(" +
            "        'icon.png', 'Notification Title', 'Notification content...');" +
            "  } else {\n" +
            "    window.webkitNotifications.requestPermission();" +
            "  } " +
            "} else { " +
            "   console.log('no notifications')" +
            "}");

Using vaadin 8

Upvotes: 0

Views: 188

Answers (1)

Oleg
Oleg

Reputation: 6314

You tried with the old api, it hasn't been supported for many versions. The new api should work:

  JavaScript.getCurrent().execute(
        "  if (!(\"Notification\" in window)) { " +
        "    alert(\"This browser does not support system notifications\"); " +
        "  } else if (Notification.permission === \"granted\") { " +
        "    new Notification(\"Hi there!\"); " +
        "  } else if (Notification.permission !== 'denied') { " +
        "    Notification.requestPermission(function (permission) { " +
        "      if (permission === \"granted\") { " +
        "        Notification(\"Hi there!\"); " +
        "      } " +
        "    }); " +
        "  } "
  );

Can't say this is a good way to do it though.

There is a plugin for vaadin 7 https://vaadin.com/directory#!addon/webnotifications you can adopt it to 8. Or create a JavaScript component or at at least a JavaScript function that will make using it easier.

Upvotes: 1

Related Questions