Search code examples
htmlgwtnotificationsjsnihtml5-notifications

GWT with HTML5 desktop notifications (webkitNotifications)


I am trying to implement HTML5 desktop notifications using GWT. Currently this is not supported with the GWT libraries, so I am using native javascript from within GWT(JSNI). I thought this would be fairly straight forward, however I am not having any success. I am using chrome and have tried in dev mode and a deployed app. Below is the code that I am using.

NOTE: the javascript code came from http://playground.html5rocks.com/#simple_notifications and it worked fine in chrome.

Has anyone got this to work??

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

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

Solution

  • Well, everthing you do looks fine to me. I tried the example and ran it with GWT and it worked. The only thing I noticed is that it can take some time till the Notification shows if you are running in debugg code:

    Here is my GWT code:

    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.');
        }
    }-*/;