Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am currently using cordova/phonegap in eclipse. I want to call the saveToken (token) in my MyFirebaseInstanceIDService.java from the JavaScript in my html file. i created a class called JavaScriptInterface.java to get the class from MyFirebaseInstanceIDService.java. How should i do this?

MyFirebaseInstanceIDService.java

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

// [START refresh_token]
@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String token = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + token);
    saveToken(token);



}

  }

JavaScriptInterface.java

public class JavaScriptInterface {
Context mContext;


JavaScriptInterface(Context c) {
   mContext = c;
}

@JavascriptInterface
public void postToken(){
    MyFirebaseInstanceIDService service = new MyFirebaseInstanceIDService();
    service.postToken();
   }
}

MainActivity.java

public class MainActivity extends CordovaActivity
{    
   loadUrl(launchUrl);
  WebView webView = (WebView) findViewById(R.id.webView1);
    webView.addJavascriptInterface(new JavaScriptInterface(this), "Token");
    }
  }

Index.html

   <script>
    function postToken(){

        service.postToken();

    }
</script>
share|improve this question
    
Sorry may i know why was i downvoted ? – amus 35 mins ago
    
I guess you need this Write a plugin in cordova – Gangadhar Jannu 18 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.