Irfan
Irfan

Reputation: 171

How to create cordova plugin for ionic

I am new to plugin creation i have searched on how to create cordova plugin and I got this link, https://blogs.oracle.com/mobile/entry/introduction_to_custom_cordova_plugin by following this link I have created a alert plugin but I am not able to use it in ionic here is the code of Alert.java placed under src/android folder.

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaArgs;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class Alert extends CordovaPlugin {
 @Override
ublic boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) 
      throws JSONException {

    if (action.equals("alert")) {
      alert(args.getString(0), args.getString(1), args.getString(2), callbackContext);
      return true;
    }
    return false;
  }

  private synchronized void alert(final String title, 
                                  final String message, 
                                  final String buttonLabel, 
                                  final CallbackContext callbackContext) {
    new AlertDialog.Builder(cordova.getActivity())
    .setTitle(title)
    .setMessage(message)
    .setCancelable(false)
    .setNeutralButton(buttonLabel, new AlertDialog.OnClickListener() {
      public void onClick(DialogInterface dialogInterface, int which) {
        dialogInterface.dismiss();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
      }
    })
    .create()
    .show();
  }
} 

here is alert.js code placed under www folder

  module.exports = {

      alert: function(title, message, buttonLabel, successCallback) {
        cordova.exec(successCallback,null,"Alert","alert",[title, message, buttonLabel]);
      }

};

plugin.xml looks like this

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
        xmlns:android="http://schemas.android.com/apk/res/android"
        id="cordova-plugin-my-alert"
        version="0.0.2">

  <name>Alert</name>
  <description>A Cordova plugin that displays an alert popup dialog</description>
  <license>Apache 2.0</license>

  <js-module src="www/Alert.js" name="Alert">
    <clobbers target="Alert" />
  </js-module>

  <!-- android -->
  <platform name="android">
    <config-file target="res/xml/config.xml" parent="/*">
      <feature name="Alert">
        <param name="android-package" value="com.acme.plugin.alert.Alert" />
      </feature>
    </config-file>
    <source-file src="src/android/Alert.java" target-dir="src/com/acme/plugin/alert" />
  </platform>
</plugin>

Please help me where I am going wrong.

Upvotes: 3

Views: 2212

Answers (1)

Irfan
Irfan

Reputation: 171

i have resolved above error by adding failure callback instead of null in alert function and also i missed out the package name in Alert.java class

Upvotes: 3

Related Questions