2011-11-01 38 views
1

我想使用GWT實現HTML5桌面通知。目前,這不支持GWT庫,所以我使用GWT(JSNI)中的原生JavaScript。我認爲這將是相當直接的,但我沒有任何成功。我正在使用chrome,並嘗試在開發模式和部署的應用程序。以下是我正在使用的代碼。GWT與HTML5桌面通知(webkitNotifications)

注意: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(); 
}-*/; 
+0

不知道,但如果這能幫助你嘗試在.html頁面的新方法中添加requestPermission(),並從gwt應用程序中調用該新方法? – Rohan

回答

3

那麼,everthing你看起來不錯。我嘗試了這個例子,並使用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 [email protected]' 
    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.'); 
    } 
}-*/; 
+0

謝謝Stefan。我沒有以異樣的方式徵求同意。那是我的問題。很棒。 – Patrick

+0

曾經有過這個問題,我拒絕了這個權限,然後它再也沒問過我(即使我調用了requestPermission();)。不得不清理我的瀏覽器現金bevore可以再次... ... – Stefan