fix openNotification crash in plugin for android

This commit is contained in:
zhangqinghe 2014-07-28 20:11:27 +08:00
parent 904384d8ae
commit 5c671fadc9
3 changed files with 59 additions and 51 deletions

View File

@ -49,7 +49,7 @@ public class JPushPlugin extends CordovaPlugin {
private static JPushPlugin instance; private static JPushPlugin instance;
public static String notificationAlert; public static String notificationAlert;
public static Map<String, String> notificationExtras=new HashMap<String, String>(); public static Map<String, Object> notificationExtras=new HashMap<String, Object>();
public JPushPlugin() { public JPushPlugin() {
instance = this; instance = this;
@ -63,7 +63,7 @@ public class JPushPlugin extends CordovaPlugin {
} }
private static JSONObject notificationObject(String message, private static JSONObject notificationObject(String message,
Map<String, String> extras) { Map<String, Object> extras) {
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
try { try {
data.put("message", message); data.put("message", message);
@ -75,7 +75,7 @@ public class JPushPlugin extends CordovaPlugin {
} }
private static JSONObject openNotificationObject(String alert, private static JSONObject openNotificationObject(String alert,
Map<String, String> extras){ Map<String, Object> extras){
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
try { try {
data.put("alert", alert); data.put("alert", alert);
@ -85,13 +85,13 @@ public class JPushPlugin extends CordovaPlugin {
} }
return data; return data;
} }
static void transmitPush(String message, Map<String, String> extras) { static void transmitPush(String message, Map<String, Object> extras) {
if (instance == null) { if (instance == null) {
return; return;
} }
JSONObject data = notificationObject(message, extras); JSONObject data = notificationObject(message, extras);
String js = String String js = String
.format("window.plugins.jPushPlugin.recieveMessageInAndroidCallback(%s);", .format("window.plugins.jPushPlugin.recieveMessageInAndroidCallback('%s');",
data.toString()); data.toString());
try { try {
instance.webView.sendJavascript(js); instance.webView.sendJavascript(js);
@ -101,13 +101,13 @@ public class JPushPlugin extends CordovaPlugin {
} }
} }
static void transmitOpen(String alert, Map<String, String> extras) { static void transmitOpen(String alert, Map<String, Object> extras) {
if (instance == null) { if (instance == null) {
return; return;
} }
JSONObject data = openNotificationObject(alert, extras); JSONObject data = openNotificationObject(alert, extras);
String js = String String js = String
.format("window.plugins.jPushPlugin.openNotificationInAndroidCallback(%s);", .format("window.plugins.jPushPlugin.openNotificationInAndroidCallback('%s');",
data.toString()); data.toString());
try { try {
instance.webView.sendJavascript(js); instance.webView.sendJavascript(js);
@ -240,6 +240,9 @@ public class JPushPlugin extends CordovaPlugin {
tagStr = data.getString(0); tagStr = data.getString(0);
String[] tagArray = tagStr.split(","); String[] tagArray = tagStr.split(",");
for (String tag : tagArray) { for (String tag : tagArray) {
if(tags==null){
tags= new HashSet<String>();
}
tags.add(tag); tags.add(tag);
} }
} }
@ -284,23 +287,23 @@ public class JPushPlugin extends CordovaPlugin {
} }
} }
void getNotification(JSONArray data, CallbackContext callBackContext) { // void getNotification(JSONArray data, CallbackContext callBackContext) {
String alert = JPushPlugin.notificationAlert; // String alert = JPushPlugin.notificationAlert;
Map<String, String> extras = JPushPlugin.notificationExtras; // Map<String, String> extras = JPushPlugin.notificationExtras;
//
JSONObject jsonData = new JSONObject(); // JSONObject jsonData = new JSONObject();
try { // try {
jsonData.put("message", alert); // jsonData.put("message", alert);
jsonData.put("extras", new JSONObject(extras)); // jsonData.put("extras", new JSONObject(extras));
} catch (JSONException e) { // } catch (JSONException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
//
callBackContext.success(jsonData); // callBackContext.success(jsonData);
//
JPushPlugin.notificationAlert = ""; // JPushPlugin.notificationAlert = "";
JPushPlugin.notificationExtras = new HashMap<String, String>(); // JPushPlugin.notificationExtras = new HashMap<String, Obl>();
} // }
void setBasicPushNotificationBuilder(JSONArray data, void setBasicPushNotificationBuilder(JSONArray data,
CallbackContext callbackContext) { CallbackContext callbackContext) {

View File

@ -35,13 +35,13 @@ public class MyReceiver extends BroadcastReceiver {
} }
private void handlingReceivedMessage(Intent intent) { private void handlingReceivedMessage(Intent intent) {
String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE); String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);
Map<String,String> extras = getNotificationExtras(intent); Map<String,Object> extras = getNotificationExtras(intent);
JPushPlugin.transmitPush(msg, extras); JPushPlugin.transmitPush(msg, extras);
} }
private void handlingNotificationOpen(Context context,Intent intent){ private void handlingNotificationOpen(Context context,Intent intent){
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
Map<String,String> extras = getNotificationExtras(intent); Map<String,Object> extras = getNotificationExtras(intent);
Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
launch.addCategory(Intent.CATEGORY_LAUNCHER); launch.addCategory(Intent.CATEGORY_LAUNCHER);
@ -54,17 +54,21 @@ public class MyReceiver extends BroadcastReceiver {
context.startActivity(launch); context.startActivity(launch);
} }
private Map<String, String> getNotificationExtras(Intent intent) { private Map<String, Object> getNotificationExtras(Intent intent) {
Map<String, String> extrasMap = new HashMap<String, String>(); Map<String, Object> extrasMap = new HashMap<String, Object>();
for (String key : intent.getExtras().keySet()) { for (String key : intent.getExtras().keySet()) {
if (!IGNORED_EXTRAS_KEYS.contains(key)) { if (!IGNORED_EXTRAS_KEYS.contains(key)) {
Log.e("key","key:"+key); Log.e("key","key:"+key);
extrasMap.put(key, intent.getStringExtra(key)); if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)){
extrasMap.put(key, intent.getIntExtra(key,0));
}else{
extrasMap.put(key, intent.getStringExtra(key));
}
} }
} }
return extrasMap; return extrasMap;
} }
private static final List<String> IGNORED_EXTRAS_KEYS = private static final List<String> 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");
} }

View File

@ -90,16 +90,16 @@ JPushPlugin.prototype.receiveMessageIniOSCallback = function(data){
} }
JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data){ JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data){
try{ try{
console.log("JPushPlugin:pushCallback--data:"+data); console.log("JPushPlugin:receiveMessageInAndroidCallback");
var bToObj=JSON.parse(data); console.log(data);
var message = bToObj.message; //var bToObj=JSON.parse(data);
var extras = bToObj.extras; //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(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){ catch(exception){
console.log("JPushPlugin:pushCallback "+exception); console.log("JPushPlugin:pushCallback "+exception);
@ -108,19 +108,20 @@ JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data){
// //
JPushPlugin.prototype.openNotificationInAndroidCallback = function(data){ JPushPlugin.prototype.openNotificationInAndroidCallback = function(data){
try{ try{
console.log("JPushPlugin:openNotificationInAndroidCallback");
console.log(data); console.log(data);
var bToObj = JSON.parse(data); //var bToObj = JSON.parse(data);
var alert = bToObj.alert; //var alert = bToObj.alert;
var extras = bToObj.extras; //var extras = bToObj.extras;
console.log(alert); //console.log(alert);
console.log(extras['cn.jpush.android.MSG_ID']); //console.log(extras['cn.jpush.android.MSG_ID']);
console.log(extras['app']); //console.log(extras['app']);
console.log(extras['cn.jpush.android.NOTIFICATION_CONTENT_TITLE']); //console.log(extras['cn.jpush.android.NOTIFICATION_CONTENT_TITLE']);
console.log(extras['cn.jpush.android.EXTRA']); //console.log(extras['cn.jpush.android.EXTRA']);
console.log(extras['cn.jpush.android.PUSH_ID']); //console.log(extras['cn.jpush.android.PUSH_ID']);
console.log(extras['cn.jpush.android.NOTIFICATION_ID']); //console.log(extras['cn.jpush.android.NOTIFICATION_ID']);
console.log("JPushPlugin:openNotificationCallback is ready"); //console.log("JPushPlugin:openNotificationCallback is ready");
} }
catch(exception){ catch(exception){
console.log(exception); console.log(exception);