0

How can I call a simple event or execute a JavaScript function in native Android code with Cordova?

I tried

    public void notificationRecieved(String heading,String news){
        try{
            if(webview!=null){
                Log.i("PUSHPARSE","in Notification Received Event");
                //webview.loadUrl("javascript:alert('URLcall')");
                webview.sendJavascript("alert('CAlling from Java')");
            }   

        }catch(Exception e1){
            Log.e("PUSHPARSE",e1.getMessage(),e1);
        }

    }

I am not able to get any notification or alert.

Update

Logcat shows I/PUSHPARSE( 2039): in Notification Received Event

Tried doing webview.loadUrl("http://www.google.com");

the logs show

>>> loadUrl(http://www.google.com)

W/CordovaWebView( 2039): CordovaWebView.init() was not called. This will soon be required.

D/CordovaWebView( 2039): CordovaWebView is running on device made by: unknown
4
  • are you sure webview is the cordova webview? and it is initialized? that code should work, or try with webview.loadUrl("javascript:console.log('test');"); and look into logcat to see if the log appears Commented Feb 3, 2015 at 12:13
  • I did that.. still no output. is there any other way to dispatch or call an event from Java to JS.? Commented Feb 3, 2015 at 12:31
  • is the Log.i("PUSHPARSE","in Notification Received Event"); being displayed on the log? Commented Feb 3, 2015 at 13:22
  • yes that gets logged. but just after the call it vanishes inside Cordova call. No exception or trace. Commented Feb 3, 2015 at 13:27

2 Answers 2

1

I guess alert is not working in webview try document.write or console.log for see

Thanks

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for reply, what do you want me to write in document? a script for alert? or so?
i guess alert('x') not working on webview may be it works webview.loadUrl("javascript:document.write('URLcall')"); or webview.sendJavascript("javascript:document.write('URLcall')"); and console.log can writes your eclipse console
0

May be you can use the pushPlugin https://github.com/phonegap-build/PushPlugin and use method function onNotification(e) ?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.