Incpetor
Incpetor

Reputation: 1303

How to call JS from native Java code?

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

Upvotes: 0

Views: 731

Answers (2)

Iron
Iron

Reputation: 2

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

Upvotes: 0

KubixSoftware
KubixSoftware

Reputation: 44

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

Thanks

Upvotes: 1

Related Questions