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;
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() {
instance = this;
@ -63,7 +63,7 @@ public class JPushPlugin extends CordovaPlugin {
}
private static JSONObject notificationObject(String message,
Map<String, String> extras) {
Map<String, Object> 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<String, String> extras){
Map<String, Object> 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<String, String> extras) {
static void transmitPush(String message, Map<String, Object> 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<String, String> extras) {
static void transmitOpen(String alert, Map<String, Object> 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<String>();
}
tags.add(tag);
}
}
@ -284,23 +287,23 @@ public class JPushPlugin extends CordovaPlugin {
}
}
void getNotification(JSONArray data, CallbackContext callBackContext) {
String alert = JPushPlugin.notificationAlert;
Map<String, String> 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<String, String>();
}
// void getNotification(JSONArray data, CallbackContext callBackContext) {
// String alert = JPushPlugin.notificationAlert;
// Map<String, String> 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<String, Obl>();
// }
void setBasicPushNotificationBuilder(JSONArray data,
CallbackContext callbackContext) {

View File

@ -35,13 +35,13 @@ public class MyReceiver extends BroadcastReceiver {
}
private void handlingReceivedMessage(Intent intent) {
String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);
Map<String,String> extras = getNotificationExtras(intent);
Map<String,Object> extras = getNotificationExtras(intent);
JPushPlugin.transmitPush(msg, extras);
}
private void handlingNotificationOpen(Context context,Intent intent){
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());
launch.addCategory(Intent.CATEGORY_LAUNCHER);
@ -54,17 +54,21 @@ public class MyReceiver extends BroadcastReceiver {
context.startActivity(launch);
}
private Map<String, String> getNotificationExtras(Intent intent) {
Map<String, String> extrasMap = new HashMap<String, String>();
private Map<String, Object> getNotificationExtras(Intent intent) {
Map<String, Object> extrasMap = new HashMap<String, Object>();
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<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){
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);