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.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;
@ -96,6 +103,84 @@ 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<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) {
HashSet<String> tags = new HashSet<String>();
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");
}
}
}

View File

@ -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 = {};
}