commit b6aa2e8d8ca095a5098f1990e7fe0e953b89a8e2 Author: Lincoln Date: Fri Oct 18 11:14:33 2013 +0800 initial project diff --git a/README.md b/README.md new file mode 100644 index 0000000..80a6d50 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +## JPush PhoneGap Plugin ## + +**MarkdownPad** 是 Windows 平台上一个功能完善的 Markdown 编辑器。 + +### Installation ### + +1.使用 phonegap 或者 cordova cli 添加插件: +`cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git` + +2.修改www/config.xml文件,添加或者覆盖以下字段 + +###Android 手工安装### +1.复制src/android/*.java 到cn/jpush/phonega/目录下(即:cn.jpush.phonegap的包下) + +2.复制src/android/ .so + +3.复制 .jar + +4.复制 .xml + +5.复制 .png + +6.修改AndroidManifest.xml 在manifest节点下添加以下权限 + +7.修改AndroidManifest.xml 在manifest/application节点下添加SDK相关组件声明 + + + + + + + diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..304ad2c --- /dev/null +++ b/plugin.xml @@ -0,0 +1,136 @@ + + + + JPush Plugin + JPush for cordova plugin + JPush + JPush,push + Apache 2.0 License + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/android/JPushPlugin.java b/src/android/JPushPlugin.java new file mode 100644 index 0000000..090e84c --- /dev/null +++ b/src/android/JPushPlugin.java @@ -0,0 +1,199 @@ +package cn.jpush.phonegap; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaInterface; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaWebView; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import android.util.Log; +import cn.jpush.android.api.BasicPushNotificationBuilder; +import cn.jpush.android.api.CustomPushNotificationBuilder; +import cn.jpush.android.api.JPushInterface; + +public class JPushPlugin extends CordovaPlugin { + private final static List methodList = + Arrays.asList( + "setTags", + "setTagAlias", + "setAlias", + "getIncoming", + "setBasicPushNotificationBuilder", + "setCustomPushNotificationBuilder"); + private ExecutorService executorService = Executors.newFixedThreadPool(1); + private static JPushPlugin instance; + + public static String incomingAlert; + public static Map incomingExtras; + + public JPushPlugin() { + instance = this; + } + + @Override + public void initialize(CordovaInterface cordova, CordovaWebView webView) { + super.initialize(cordova, webView); + JPushInterface.setDebugMode(true); + JPushInterface.init(cordova.getActivity().getApplicationContext()); + } + + private static JSONObject notificationObject(String message, + Map extras) { + JSONObject data = new JSONObject(); + try { + data.put("message", message); + data.put("extras", new JSONObject(extras)); + } catch (JSONException e) { + + } + return data; + } + + static void raisePush(String message, Map extras) { + if (instance == null) { + return; + } + JSONObject data = notificationObject(message, extras); + String js = String + .format("window.plugins.jPushPlugin.pushCallback(%s);", + data.toString()); + try { + instance.webView.sendJavascript(js); + } catch (NullPointerException e) { + + } catch (Exception e) { + + } + } + + @Override + public boolean execute(final String action, final JSONArray data, + final CallbackContext callbackContext) throws JSONException { + if (!methodList.contains(action)) { + return false; + } + executorService.execute(new Runnable() { + @Override + public void run() { + try { + Method method = JPushPlugin.class.getDeclaredMethod(action, + JSONArray.class, CallbackContext.class); + method.invoke(JPushPlugin.this, data, callbackContext); + } catch (Exception e) { + } + } + }); + return true; + } + + void setTags(JSONArray data, CallbackContext callbackContext) { + HashSet tags = new HashSet(); + try { + String tagStr = data.getString(0); + String[] tagArr = tagStr.split(","); + for (String tag : tagArr) { + tags.add(tag); + } + Set validTags = JPushInterface.filterValidTags(tags); + JPushInterface.setTags(this.cordova.getActivity() + .getApplicationContext(), validTags, null); + callbackContext.success(); + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("Error reading tags JSON"); + } + } + + void setAlias(JSONArray data, CallbackContext callbackContext) { + Log.e("lincoln", "set alias start"); + try { + String alias = data.getString(0); + JPushInterface.setAlias(this.cordova.getActivity() + .getApplicationContext(), alias, null); + Log.e("lincoln", "set alias:" + alias); + callbackContext.success(); + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("Error reading alias JSON"); + } + } + + void setTagAlias(JSONArray data, CallbackContext callbackContext) { + HashSet tags = new HashSet(); + String alias; + try { + alias = data.getString(0); + JSONArray tagsArr = data.getJSONArray(1); + for (int i = 0; i < tagsArr.length(); i++) { + tags.add(tagsArr.getString(i)); + } + + JPushInterface.setAliasAndTags(this.cordova.getActivity() + .getApplicationContext(), alias, tags); + callbackContext.success(); + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("Error reading tagAlias JSON"); + } + } + + void getIncoming(JSONArray data, CallbackContext callBackContext) { + String alert = JPushPlugin.incomingAlert; + Map extras = JPushPlugin.incomingExtras; + + JSONObject jsonData = new JSONObject(); + try { + jsonData.put("message", alert); + jsonData.put("extras", new JSONObject(extras)); + } catch (JSONException e) { + e.printStackTrace(); + } + + callBackContext.success(jsonData); + + JPushPlugin.incomingAlert = ""; + JPushPlugin.incomingExtras = new HashMap(); + } + + void setBasicPushNotificationBuilder(JSONArray data, + CallbackContext callbackContext) { + BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder( + this.cordova.getActivity()); + builder.developerArg0 = "Basic builder 1"; + JPushInterface.setPushNotificationBuilder(1, builder); + JSONObject obj = new JSONObject(); + try { + obj.put("id", 1); + } catch (JSONException e) { + e.printStackTrace(); + } + callbackContext.success(obj); + } + + void setCustomPushNotificationBuilder(JSONArray data, + CallbackContext callbackContext) { + CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder( + this.cordova.getActivity(), R.layout.test_notitfication_layout, + R.id.icon, R.id.title, R.id.text); + builder.developerArg0 = "Custom Builder 1"; + builder.layoutIconDrawable = R.drawable.jpush_notification_icon; + JPushInterface.setPushNotificationBuilder(2, builder); + JSONObject obj = new JSONObject(); + try { + obj.put("id", 2); + } catch (JSONException e) { + e.printStackTrace(); + } + callbackContext.success(obj); + } +} diff --git a/src/android/MyReceiver.java b/src/android/MyReceiver.java new file mode 100644 index 0000000..15c7cc5 --- /dev/null +++ b/src/android/MyReceiver.java @@ -0,0 +1,68 @@ +package cn.jpush.phonegap; + + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import cn.jpush.android.api.JPushInterface; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +public class MyReceiver extends BroadcastReceiver { + private static String TAG = "Client Receiver"; + @Override + public void onReceive(Context context, Intent intent) { + + if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { + + }else if (JPushInterface.ACTION_UNREGISTER.equals(intent.getAction())){ + + } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { + handlingReceivedMessage(intent); + } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { + + } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { + handlingNotificationOpen(context,intent); + } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) { + + } else { + Log.d(TAG, "Unhandled intent - " + intent.getAction()); + } + + } + private void handlingReceivedMessage(Intent intent) { + String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE); + Map extras = getNotificationExtras(intent); + + JPushPlugin.raisePush(msg, extras); + } + private void handlingNotificationOpen(Context context,Intent intent){ + String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); + Map extras = getNotificationExtras(intent); + + Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); + launch.addCategory(Intent.CATEGORY_LAUNCHER); + launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP); + + JPushPlugin.incomingAlert = alert; + JPushPlugin.incomingExtras = extras; + + context.startActivity(launch); + } + private Map getNotificationExtras(Intent intent) { + Map extrasMap = new HashMap(); + + for (String key : intent.getExtras().keySet()) { + if (!IGNORED_EXTRAS_KEYS.contains(key)) { + Log.e("key","key:"+key); + extrasMap.put(key, intent.getStringExtra(key)); + } + } + return extrasMap; + } + private static final List IGNORED_EXTRAS_KEYS = + Arrays.asList("cn.jpush.android.TITLE","cn.jpush.android.MESSAGE","cn.jpush.android.APPKEY"); +} diff --git a/src/android/jpush-sdk-release1.5.0.jar b/src/android/jpush-sdk-release1.5.0.jar new file mode 100644 index 0000000..928788d Binary files /dev/null and b/src/android/jpush-sdk-release1.5.0.jar differ diff --git a/src/android/jpush_notification_icon.png b/src/android/jpush_notification_icon.png new file mode 100644 index 0000000..bbb2340 Binary files /dev/null and b/src/android/jpush_notification_icon.png differ diff --git a/src/android/libjpush.so b/src/android/libjpush.so new file mode 100644 index 0000000..84aca38 Binary files /dev/null and b/src/android/libjpush.so differ diff --git a/src/android/test_notitfication_layout.xml b/src/android/test_notitfication_layout.xml new file mode 100644 index 0000000..556c45c --- /dev/null +++ b/src/android/test_notitfication_layout.xml @@ -0,0 +1,24 @@ + + + + + + + + \ No newline at end of file diff --git a/www/JPushPlugin.js b/www/JPushPlugin.js new file mode 100644 index 0000000..4396dbf --- /dev/null +++ b/www/JPushPlugin.js @@ -0,0 +1,46 @@ +var JPushPlugin = function(){ + +}; +JPushPlugin.prototype.failure = function (msg) { + console.log("Javascript Callback Error: " + msg) +} +JPushPlugin.prototype.call_native = function (callback, name, args) { + if(arguments.length == 2) { + args = [] + } + ret = cordova.exec( + callback, // called when signature capture is successful + this.failure, // called when signature capture encounters an error + 'JPushPlugin', // Tell cordova that we want to run "JPushPlugin" + name, // Tell the plugin the action we want to perform + args); // List of arguments to the plugin + return ret; +} +JPushPlugin.prototype.setTags = function (tags, callback) { + this.call_native(callback, "setTags", [tags]) +} +JPushPlugin.prototype.setAlias = function (alias, callback) { + this.call_native(callback, "setAlias", [alias]) +} +JPushPlugin.prototype.pushCallback = function (data) { + var strArr = [data] + var str = strArr[0].message + document.getElementById('tarea').value=str + +} +JPushPlugin.prototype.getIncoming = function (callback) { + this.call_native(callback, "getIncoming"); +} +JPushPlugin.prototype.setBasicPushNotificationBuilder = function(callback){ + this.call_native(callback,"setBasicPushNotificationBuilder"); +} +JPushPlugin.prototype.setCustomPushNotificationBuilder = function(callback){ + this.call_native(callback,"setCustomPushNotificationBuilder"); +} +// Register the plugin +if(!window.plugins) { + window.plugins = {}; +} +if(!window.plugins.jPushPlugin){ + window.plugins.jPushPlugin = new JPushPlugin(); +} \ No newline at end of file