【问题标题】:Vaadin Desktop NotificationsVaadin 桌面通知
【发布时间】:2017-08-16 20:14:23
【问题描述】:

Vaadin 是否支持 html5 浏览器“桌面通知”?我一直在寻找这个,但找不到任何具体的内容。

我尝试过这样的事情,但没有运气。

        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')" +
            "}");

使用 vaadin 8

【问题讨论】:

    标签: vaadin


    【解决方案1】:

    您尝试使用旧的 api,许多版本都不支持它。 new api 应该可以工作:

      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!\"); " +
            "      } " +
            "    }); " +
            "  } "
      );
    

    虽然不能说这是一个好方法。

    有一个用于 vaadin 7 https://vaadin.com/directory#!addon/webnotifications 的插件,您可以将其应用到 8。或者创建一个 JavaScript component 或至少一个 JavaScript function,这样可以更轻松地使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      • 2016-02-10
      • 1970-01-01
      相关资源
      最近更新 更多