1

GWTを使用してHTML5デスクトップ通知を実装しようとしています。現在、これはGWTライブラリではサポートされていないため、GWT(JSNI)内からネイティブJavaScriptを使用しています。これはかなり簡単だと思いましたが、うまくいきませんでした。私はChromeを使用しており、開発モードとデプロイされたアプリで試しました。以下は私が使用しているコードです。

注:javascriptコードはhttp://playground.html5rocks.com/#simple_notificationsからのものであり、Chromeでは正常に機能しました。

誰かがこれを機能させましたか?

 public native void requestPermission() /*-{
         $wnd.webkitNotifications.requestPermission();      
     }-*/;

  public native void createJSNotification(String iconUrl, String title, String body) /*-{
    $wnd.webkitNotifications.createNotification(iconUrl, title, body).show();
}-*/;
4

1 に答える 1

3

ええと、あなたがすることはすべて私にはうまく見えます。この例を試し、GWTで実行したところ、機能しました。私が気付いた唯一のことは、debuggコードで実行している場合、通知が表示されるまでに時間がかかる可能性があることです。

これが私のGWTコードです:

public void onModuleLoad() {
    {
        Button bt_Permission = new Button("Request Permission");
        bt_Permission.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                requestPermission();
            }
        });
        RootPanel.get().add(bt_Permission);
    }
    {
        Button bt_ShowNotification = new Button("Show Notification");
        bt_ShowNotification.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                showNotification();
            }
        });
        RootPanel.get().add(bt_ShowNotification);
    }
}

public native void requestPermission() /*-{
    $wnd.webkitNotifications.requestPermission();
}-*/;

public native void  showNotification() /*-{
    var text = 'You got a new email from someone@test.com'
    if ($wnd.webkitNotifications.checkPermission() == 0) {
        // note the show()
        $wnd.webkitNotifications.createNotification('',
                'Plain Text Notification', text).show();
    } else {
        alert('You have to click on "Set notification permissions for this page" first to be able to receive notifications.');
    }
}-*/;
于 2011-11-02T10:59:02.863 に答える