add features

This commit is contained in:
Lincoln 2013-10-21 11:28:42 +08:00
parent 83a491c7a4
commit 134d721aaf
2 changed files with 151 additions and 12 deletions

View File

@ -16,7 +16,6 @@ import org.apache.cordova.CordovaWebView;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.util.Log;
import cn.jpush.android.api.BasicPushNotificationBuilder; import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.CustomPushNotificationBuilder; import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface; import cn.jpush.android.api.JPushInterface;
@ -29,7 +28,15 @@ public class JPushPlugin extends CordovaPlugin {
"setAlias", "setAlias",
"getIncoming", "getIncoming",
"setBasicPushNotificationBuilder", "setBasicPushNotificationBuilder",
"setCustomPushNotificationBuilder"); "setCustomPushNotificationBuilder",
"setPushTime",
"init",
"setDebugable",
"stopPush",
"resumePush",
"isPushStopped",
"setLatestNotificationNum",
"setPushTime");
private ExecutorService executorService = Executors.newFixedThreadPool(1); private ExecutorService executorService = Executors.newFixedThreadPool(1);
private static JPushPlugin instance; private static JPushPlugin instance;
@ -96,6 +103,84 @@ public class JPushPlugin extends CordovaPlugin {
return true; 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<Integer> days = new HashSet<Integer>();
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) { void setTags(JSONArray data, CallbackContext callbackContext) {
HashSet<String> tags = new HashSet<String>(); HashSet<String> tags = new HashSet<String>();
try { try {
@ -115,12 +200,10 @@ public class JPushPlugin extends CordovaPlugin {
} }
void setAlias(JSONArray data, CallbackContext callbackContext) { void setAlias(JSONArray data, CallbackContext callbackContext) {
Log.e("lincoln", "set alias start");
try { try {
String alias = data.getString(0); String alias = data.getString(0);
JPushInterface.setAlias(this.cordova.getActivity() JPushInterface.setAlias(this.cordova.getActivity()
.getApplicationContext(), alias, null); .getApplicationContext(), alias, null);
Log.e("lincoln", "set alias:" + alias);
callbackContext.success(); callbackContext.success();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -196,4 +279,24 @@ public class JPushPlugin extends CordovaPlugin {
} }
callbackContext.success(obj); 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");
}
}
} }

View File

@ -1,7 +1,7 @@
var JPushPlugin = function(){ var JPushPlugin = function(){
}; };
JPushPlugin.prototype.failure = function (msg) { JPushPlugin.prototype.error_callback = function (msg) {
console.log("Javascript Callback Error: " + msg) console.log("Javascript Callback Error: " + msg)
} }
JPushPlugin.prototype.call_native = function (callback, name, args) { JPushPlugin.prototype.call_native = function (callback, name, args) {
@ -9,35 +9,71 @@ JPushPlugin.prototype.call_native = function (callback, name, args) {
args = [] args = []
} }
ret = cordova.exec( ret = cordova.exec(
callback, // called when signature capture is successful callback,
this.failure, // called when signature capture encounters an error this.error_callback,
'JPushPlugin', // Tell cordova that we want to run "JPushPlugin" 'JPushPlugin',
name, // Tell the plugin the action we want to perform name,
args); // List of arguments to the plugin args);
return ret; return ret;
} }
JPushPlugin.prototype.setTags = function (tags, callback) { JPushPlugin.prototype.setTags = function (tags, callback) {
this.call_native(callback, "setTags", [tags]) this.call_native(callback, "setTags", [tags])
} }
JPushPlugin.prototype.setAlias = function (alias, callback) { JPushPlugin.prototype.setAlias = function (alias, callback) {
this.call_native(callback, "setAlias", [alias]) this.call_native(callback, "setAlias", [alias])
} }
JPushPlugin.prototype.pushCallback = function (data) { JPushPlugin.prototype.pushCallback = function (data) {
var strArr = [data] var strArr = [data]
var str = strArr[0].message var str = strArr[0].message
document.getElementById('tarea').value=str document.getElementById('tarea').value=str
} }
JPushPlugin.prototype.getIncoming = function (callback) { JPushPlugin.prototype.getIncoming = function (callback) {
this.call_native(callback, "getIncoming"); this.call_native(callback, "getIncoming");
} }
JPushPlugin.prototype.setBasicPushNotificationBuilder = function(callback){ JPushPlugin.prototype.setBasicPushNotificationBuilder = function(callback){
this.call_native(callback,"setBasicPushNotificationBuilder"); this.call_native(callback,"setBasicPushNotificationBuilder");
} }
JPushPlugin.prototype.setCustomPushNotificationBuilder = function(callback){ JPushPlugin.prototype.setCustomPushNotificationBuilder = function(callback){
this.call_native(callback,"setCustomPushNotificationBuilder"); 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) { if(!window.plugins) {
window.plugins = {}; window.plugins = {};
} }