Search code examples
javajavascriptgwtphonegap-pluginsjsni

Use native Phonegape js in GWT using JSNI


I wanna run this phonegap native alert on GWT using JSNI

navigator.notification.alert(
        'You are the winner!',  // message
         null,         // callback
        'Game Over',
        'Done'
  );

I've tried this:

public static native void testNativeAlert()/*-{

   navigator.notification.alert(
    'You are the winner!',  // message
     null,         // callback
    'Game Over',
    'Done');}-*/;

Solution

  • From http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#writing, don't forget about $wnd to access global properties!

    public static native void alert(String msg) /*-{
      $wnd.alert(msg);
    }-*/;
    

    Note that the code did not reference the JavaScript window object directly inside the method. When accessing the browser’s window and document objects from JSNI, you must reference them as $wnd and $doc, respectively. Your compiled script runs in a nested frame, and $wnd and $doc are automatically initialized to correctly refer to the host page’s window and document.

    So in your case, you have to use $wnd to access navigator:

    public static native void testNativeAlert()/*-{
    
       $wnd.navigator.notification.alert(
        'You are the winner!',  // message
        null,         // callback
        'Game Over',
        'Done');
    }-*/;
    

    Side note: the docs aren't clear, but are you sure that phonegap lets callback be null? Otherwise look into that JSNI docs I linked above further.