SendJavascript function not defined error in Cordoba Salesforce Plugin

I am trying to create a Salesforce plugin in which I want to return data to JS from my java code (which is basically a background process) But it always gives me an error for the line this.sendJavascript (function name)

Below is the plugin code -

package com.salesforce.androidsdk.phonegap; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import android.content.Intent; import android.util.Log; /** * This class echoes a string called from JavaScript. */ public class Echo extends CordovaPlugin { private static final String TAG = "CordovaPlugin"; @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { Log.i(TAG,"inside execute method--->>>" + action + args + callbackContext); if (action.trim().equalsIgnoreCase("echo")) { Log.i(TAG,"args.getString(0)--->>>" + args.getString(0)); String message = args.getString(0); // Initialise the service variables and start it it up Context thiscontext = this.cordova.getActivity().getApplicationContext(); Intent callBackgroundService = new Intent(thiscontext, CallBackgroundService.class); callBackgroundService.putExtra("loadinterval", 800); // Set LED flash interval thiscontext.startService(callBackgroundService); this.echo(message, callbackContext); sendValue("Kaushik", "Ray"); return true; } return false; } private void echo(String message, CallbackContext callbackContext) { if (message != null && message.length() > 0) { callbackContext.success(message); } else { callbackContext.error("Expected one non-empty string argument."); } } public void sendValue(String value1, String value2) { JSONObject data = new JSONObject(); try { data.put("value1", "Kaushik"); data.put("value2", "Ray"); } catch (JSONException e) { Log.e("CommTest", e.getMessage()); } String js = String.format( "window.plugins.commtest.updateValues('%s');", data.toString()); error line ------->>> this.sendJavascript(js); } } 

I noted the error line that is at the end. Please help me solve this problem.

Thanks Advance, Cat

0
source share
1 answer

sendJavascript() is a function in both DroidGap.java and CordovaWebView.java, but the value of this in your current file is your instance of Echo. The string this.sendJavascript() fails because it expects a function called an Echo member or a public / protected member of one of the classes inherited from it, in this case CordovaPlugin.

CordovaPlugin.java has a public variable called webView , which is the CordovaWebView for your project. If you change the line with a violation from this.sendJavascript(js) to webView.sendJavascript(js) , it will fix your error.

+1
source

Source: https://habr.com/ru/post/1435288/


All Articles