From 5c671fadc973fb603e2040801ade8130d8ccd8cd Mon Sep 17 00:00:00 2001 From: zhangqinghe Date: Mon, 28 Jul 2014 20:11:27 +0800 Subject: [PATCH] fix openNotification crash in plugin for android --- src/android/JPushPlugin.java | 51 +++++++++++++++++++----------------- src/android/MyReceiver.java | 18 ++++++++----- www/JPushPlugin.js | 41 +++++++++++++++-------------- 3 files changed, 59 insertions(+), 51 deletions(-) diff --git a/src/android/JPushPlugin.java b/src/android/JPushPlugin.java index d2ee1f2..b8af503 100644 --- a/src/android/JPushPlugin.java +++ b/src/android/JPushPlugin.java @@ -49,7 +49,7 @@ public class JPushPlugin extends CordovaPlugin { private static JPushPlugin instance; public static String notificationAlert; - public static Map notificationExtras=new HashMap(); + public static Map notificationExtras=new HashMap(); public JPushPlugin() { instance = this; @@ -63,7 +63,7 @@ public class JPushPlugin extends CordovaPlugin { } private static JSONObject notificationObject(String message, - Map extras) { + Map extras) { JSONObject data = new JSONObject(); try { data.put("message", message); @@ -75,7 +75,7 @@ public class JPushPlugin extends CordovaPlugin { } private static JSONObject openNotificationObject(String alert, - Map extras){ + Map extras){ JSONObject data = new JSONObject(); try { data.put("alert", alert); @@ -85,13 +85,13 @@ public class JPushPlugin extends CordovaPlugin { } return data; } - static void transmitPush(String message, Map extras) { + static void transmitPush(String message, Map extras) { if (instance == null) { return; } JSONObject data = notificationObject(message, extras); String js = String - .format("window.plugins.jPushPlugin.recieveMessageInAndroidCallback(%s);", + .format("window.plugins.jPushPlugin.recieveMessageInAndroidCallback('%s');", data.toString()); try { instance.webView.sendJavascript(js); @@ -101,13 +101,13 @@ public class JPushPlugin extends CordovaPlugin { } } - static void transmitOpen(String alert, Map extras) { + static void transmitOpen(String alert, Map extras) { if (instance == null) { return; } JSONObject data = openNotificationObject(alert, extras); String js = String - .format("window.plugins.jPushPlugin.openNotificationInAndroidCallback(%s);", + .format("window.plugins.jPushPlugin.openNotificationInAndroidCallback('%s');", data.toString()); try { instance.webView.sendJavascript(js); @@ -240,6 +240,9 @@ public class JPushPlugin extends CordovaPlugin { tagStr = data.getString(0); String[] tagArray = tagStr.split(","); for (String tag : tagArray) { + if(tags==null){ + tags= new HashSet(); + } tags.add(tag); } } @@ -284,23 +287,23 @@ public class JPushPlugin extends CordovaPlugin { } } - void getNotification(JSONArray data, CallbackContext callBackContext) { - String alert = JPushPlugin.notificationAlert; - Map extras = JPushPlugin.notificationExtras; - - JSONObject jsonData = new JSONObject(); - try { - jsonData.put("message", alert); - jsonData.put("extras", new JSONObject(extras)); - } catch (JSONException e) { - e.printStackTrace(); - } - - callBackContext.success(jsonData); - - JPushPlugin.notificationAlert = ""; - JPushPlugin.notificationExtras = new HashMap(); - } +// void getNotification(JSONArray data, CallbackContext callBackContext) { +// String alert = JPushPlugin.notificationAlert; +// Map extras = JPushPlugin.notificationExtras; +// +// JSONObject jsonData = new JSONObject(); +// try { +// jsonData.put("message", alert); +// jsonData.put("extras", new JSONObject(extras)); +// } catch (JSONException e) { +// e.printStackTrace(); +// } +// +// callBackContext.success(jsonData); +// +// JPushPlugin.notificationAlert = ""; +// JPushPlugin.notificationExtras = new HashMap(); +// } void setBasicPushNotificationBuilder(JSONArray data, CallbackContext callbackContext) { diff --git a/src/android/MyReceiver.java b/src/android/MyReceiver.java index ce2426f..db07e69 100644 --- a/src/android/MyReceiver.java +++ b/src/android/MyReceiver.java @@ -35,13 +35,13 @@ public class MyReceiver extends BroadcastReceiver { } private void handlingReceivedMessage(Intent intent) { String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE); - Map extras = getNotificationExtras(intent); + Map extras = getNotificationExtras(intent); JPushPlugin.transmitPush(msg, extras); } private void handlingNotificationOpen(Context context,Intent intent){ String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); - Map extras = getNotificationExtras(intent); + Map extras = getNotificationExtras(intent); Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); launch.addCategory(Intent.CATEGORY_LAUNCHER); @@ -54,17 +54,21 @@ public class MyReceiver extends BroadcastReceiver { context.startActivity(launch); } - private Map getNotificationExtras(Intent intent) { - Map extrasMap = new HashMap(); + 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)); + Log.e("key","key:"+key); + if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)){ + extrasMap.put(key, intent.getIntExtra(key,0)); + }else{ + 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"); + Arrays.asList("cn.jpush.android.TITLE","cn.jpush.android.MESSAGE","cn.jpush.android.APPKEY","cn.jpush.android.NOTIFICATION_CONTENT_TITLE"); } diff --git a/www/JPushPlugin.js b/www/JPushPlugin.js index 29c2a40..8250cba 100644 --- a/www/JPushPlugin.js +++ b/www/JPushPlugin.js @@ -90,16 +90,16 @@ JPushPlugin.prototype.receiveMessageIniOSCallback = function(data){ } JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data){ try{ - console.log("JPushPlugin:pushCallback--data:"+data); - var bToObj=JSON.parse(data); - var message = bToObj.message; - var extras = bToObj.extras; - - console.log(message); - console.log(extras['cn.jpush.android.MSG_ID']); - console.log(extras['cn.jpush.android.CONTENT_TYPE']); - console.log(extras['cn.jpush.android.EXTRA']); + console.log("JPushPlugin:receiveMessageInAndroidCallback"); + console.log(data); + //var bToObj=JSON.parse(data); + //var message = bToObj.message; + //var extras = bToObj.extras; + //console.log(message); + //console.log(extras['cn.jpush.android.MSG_ID']); + //console.log(extras['cn.jpush.android.CONTENT_TYPE']); + //console.log(extras['cn.jpush.android.EXTRA']); } catch(exception){ console.log("JPushPlugin:pushCallback "+exception); @@ -108,19 +108,20 @@ JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data){ // JPushPlugin.prototype.openNotificationInAndroidCallback = function(data){ try{ + console.log("JPushPlugin:openNotificationInAndroidCallback"); console.log(data); - var bToObj = JSON.parse(data); - var alert = bToObj.alert; - var extras = bToObj.extras; - console.log(alert); + //var bToObj = JSON.parse(data); + //var alert = bToObj.alert; + //var extras = bToObj.extras; + //console.log(alert); - console.log(extras['cn.jpush.android.MSG_ID']); - console.log(extras['app']); - console.log(extras['cn.jpush.android.NOTIFICATION_CONTENT_TITLE']); - console.log(extras['cn.jpush.android.EXTRA']); - console.log(extras['cn.jpush.android.PUSH_ID']); - console.log(extras['cn.jpush.android.NOTIFICATION_ID']); - console.log("JPushPlugin:openNotificationCallback is ready"); + //console.log(extras['cn.jpush.android.MSG_ID']); + //console.log(extras['app']); + //console.log(extras['cn.jpush.android.NOTIFICATION_CONTENT_TITLE']); + //console.log(extras['cn.jpush.android.EXTRA']); + //console.log(extras['cn.jpush.android.PUSH_ID']); + //console.log(extras['cn.jpush.android.NOTIFICATION_ID']); + //console.log("JPushPlugin:openNotificationCallback is ready"); } catch(exception){ console.log(exception);