From 134d721aafddafd6e3283af3cb086598f787a25e Mon Sep 17 00:00:00 2001 From: Lincoln Date: Mon, 21 Oct 2013 11:28:42 +0800 Subject: [PATCH] add features --- src/android/JPushPlugin.java | 113 +++++++++++++++++++++++++++++++++-- www/JPushPlugin.js | 50 +++++++++++++--- 2 files changed, 151 insertions(+), 12 deletions(-) diff --git a/src/android/JPushPlugin.java b/src/android/JPushPlugin.java index 090e84c..6c3f841 100644 --- a/src/android/JPushPlugin.java +++ b/src/android/JPushPlugin.java @@ -16,7 +16,6 @@ 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; @@ -29,7 +28,15 @@ public class JPushPlugin extends CordovaPlugin { "setAlias", "getIncoming", "setBasicPushNotificationBuilder", - "setCustomPushNotificationBuilder"); + "setCustomPushNotificationBuilder", + "setPushTime", + "init", + "setDebugable", + "stopPush", + "resumePush", + "isPushStopped", + "setLatestNotificationNum", + "setPushTime"); private ExecutorService executorService = Executors.newFixedThreadPool(1); private static JPushPlugin instance; @@ -95,7 +102,85 @@ public class JPushPlugin extends CordovaPlugin { }); return true; } - + + void init(JSONArray data, + CallbackContext callbackContext){ + JPushInterface.init(this.cordova.getActivity().getApplicationContext()); + callbackContext.success(); + } + + void setDebugable(JSONArray data, + CallbackContext callbackContext){ + String mode = data.getString(0); + if(mode.equals(true)){ + JPushInterface.setDebugMode(true); + }else if(mode.equals(false)){ + JPushInterface.setDebugMode(false); + }else{ + callbackContext.error("error mode"); + } + callbackContext.success(); + } + + void stopPush(JSONArray data, + CallbackContext callbackContext){ + JPushInterface.stopPush(this.cordova.getActivity().getApplicationContext()); + callbackContext.success(); + } + + void resumePush(JSONArray data, + CallbackContext callbackContext){ + JPushInterface.resumePush(this.cordova.getActivity().getApplicationContext()); + callbackContext.success(); + } + + void isPushStopped(JSONArray data, + CallbackContext callbackContext){ + JPushInterface.isPushStopped(this.cordova.getActivity().getApplicationContext()); + callbackContext.success(); + } + + void setLatestNotificationNum(JSONArray data, + CallbackContext callbackContext){ + int num = -1; + try { + num = data.getInt(0); + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("error reading num json"); + } + if(num != -1){ + JPushInterface.setLatestNotifactionNumber(this.cordova.getActivity().getApplicationContext(), num); + }else{ + callbackContext.error("error num"); + } + } + + void setPushTime(JSONArray data, + CallbackContext callbackContext){ + Set days = new HashSet(); + JSONArray dayArr; + int startHour = -1; + int endHour = -1; + try { + dayArr = data.getJSONArray(0); + for (int i = 0; i < dayArr.length(); i++) { + days.add(dayArr.getInt(i)); + } + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("error reading days json"); + } + try{ + startHour = data.getInt(1); + endHour = data.getInt(2); + }catch(JSONException e){ + callbackContext.error("error reading hour json"); + } + JPushInterface.setPushTime(this.cordova.getActivity().getApplicationContext(), days, startHour, endHour); + callbackContext.success(); + } + void setTags(JSONArray data, CallbackContext callbackContext) { HashSet tags = new HashSet(); try { @@ -115,12 +200,10 @@ public class JPushPlugin extends CordovaPlugin { } 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(); @@ -196,4 +279,24 @@ public class JPushPlugin extends CordovaPlugin { } callbackContext.success(obj); } + void clearAllNotification(JSONArray data, + CallbackContext callbackContext){ + JPushInterface.clearAllNotifications(this.cordova.getActivity()); + callbackContext.success(); + } + void clearNotificationById(JSONArray data, + CallbackContext callbackContext){ + int notificationId=-1; + try { + notificationId = data.getInt(0); + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("error reading id json"); + } + if(notificationId != -1){ + JPushInterface.clearNotificationById(this.cordova.getActivity(), notificationId); + }else{ + callbackContext.error("error id"); + } + } } diff --git a/www/JPushPlugin.js b/www/JPushPlugin.js index 4396dbf..87104fd 100644 --- a/www/JPushPlugin.js +++ b/www/JPushPlugin.js @@ -1,7 +1,7 @@ var JPushPlugin = function(){ }; -JPushPlugin.prototype.failure = function (msg) { +JPushPlugin.prototype.error_callback = function (msg) { console.log("Javascript Callback Error: " + msg) } JPushPlugin.prototype.call_native = function (callback, name, args) { @@ -9,35 +9,71 @@ JPushPlugin.prototype.call_native = function (callback, name, args) { 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 + callback, + this.error_callback, + 'JPushPlugin', + name, + args); 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 + +JPushPlugin.prototype.stopPush = function(callback){ + this.call_native(callback,"stopPush"); +} + +JPushPlugin.prototype.resumePush = function(callback){ + this.call_native(callback,"resumePush"); +} + +JPushPlugin.prototype.clearAllNoticication = function(callback){ + this.call_native(callback,"clearAllNotification"); +} + +JPushPlugin.prototype.setLatestNotificationNum = function(num,callback){ + this.call_native(callback,"setLatestNotificationNum",[num]); +} + +JPushPlugin.prototype.isPushStopped = function(callback){ + this.call_native(callback,"isPushStopped") +} + +JPushPlugin.prototype.init = function(callback){ + this.call_natvie(callback,"init"); +} + +JPushPlugin.prototype.setDebugable = function(mode,callback){ + this.call_native(callback,"setDebugable",[mode]); +} + + + if(!window.plugins) { window.plugins = {}; }