From 0d64cb853b1f9d8f8bb1dbac28d680b2b5b5a753 Mon Sep 17 00:00:00 2001 From: LcTerry Date: Wed, 12 Feb 2025 11:59:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E9=B8=BF=E8=92=99?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E4=B8=8A=E5=90=8E=E5=8F=B0=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E6=94=B6=E5=88=B0=E6=B6=88=E6=81=AF=E9=97=AE=E9=A2=98=E3=80=82?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E9=87=87=E7=94=A8JPushMessageReceiver?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=9B=9E=E8=B0=83=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/android/JPushEventReceiver.java | 68 +++++++++--------- src/android/JPushPlugin.java | 42 +++++------ src/android/JPushReceiver.java | 104 ++++++++++++++-------------- 3 files changed, 105 insertions(+), 109 deletions(-) diff --git a/src/android/JPushEventReceiver.java b/src/android/JPushEventReceiver.java index ce27ba5..54786c5 100644 --- a/src/android/JPushEventReceiver.java +++ b/src/android/JPushEventReceiver.java @@ -89,36 +89,38 @@ public class JPushEventReceiver extends JPushMessageReceiver { public void onMessage(Context context, CustomMessage customMessage) { // super.onMessage(context,customMessage); cn.jiguang.cordova.push.JLogger.d(TAG,"onMessage:"+customMessage); + try { + JSONObject jsonObject=new JSONObject(); + jsonObject.put("message", customMessage.message); + jsonObject.put("alert", customMessage.title); + jsonObject.put(JPushInterface.EXTRA_EXTRA, stringToMap(customMessage.extra)); + jsonObject.put(JPushInterface.EXTRA_MSG_ID, customMessage.messageId); + jsonObject.put(JPushInterface.EXTRA_CONTENT_TYPE, customMessage.contentType); + if (JPushConstants.SDK_VERSION_CODE >= 387) { + jsonObject.put(JPushInterface.EXTRA_TYPE_PLATFORM, customMessage.platform); + } + cn.jiguang.cordova.push.JPushPlugin.transmitNotificationReceive(jsonObject); + }catch (Throwable throwable){ + cn.jiguang.cordova.push.JLogger.d(TAG,"onMessage throwable:"+throwable); - JPushPlugin.transmitMessageReceive(customMessage.message, getExtras(customMessage)); - -// String msg = customMessage.message;//intent.getStringExtra(JPushInterface.EXTRA_MESSAGE); -// Map extras = getNotificationExtras(intent); -// JPushPlugin.transmitMessageReceive(msg, extras); - } - private Map getExtras(CustomMessage customMessage) { - Map extra = new HashMap<>(); - extra.put(JPushInterface.EXTRA_EXTRA, stringToMap(customMessage.extra)); - extra.put(JPushInterface.EXTRA_MSG_ID, customMessage.messageId); - extra.put(JPushInterface.EXTRA_CONTENT_TYPE, customMessage.contentType); - if (JPushConstants.SDK_VERSION_CODE >= 387) { - extra.put(JPushInterface.EXTRA_TYPE_PLATFORM, customMessage.platform); } - return extra; } + @Override public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) { // super.onNotifyMessageArrived(context, notificationMessage); cn.jiguang.cordova.push.JLogger.d(TAG,"onNotifyMessageArrived:"+notificationMessage); - String title = notificationMessage.notificationTitle; - JPushPlugin.notificationTitle = title; + try { + JSONObject jsonObject=new JSONObject(); + jsonObject.put("title", notificationMessage.notificationTitle); + jsonObject.put("alert", notificationMessage.notificationContent); + getExtras(jsonObject,notificationMessage); + JPushPlugin.notificationJson = jsonObject; + cn.jiguang.cordova.push.JPushPlugin.transmitNotificationReceive(jsonObject); + }catch (Throwable throwable){ + cn.jiguang.cordova.push.JLogger.d(TAG,"onNotifyMessageArrived throwable:"+throwable); - String alert = notificationMessage.notificationContent; - JPushPlugin.notificationAlert = alert; - - Map extras = getExtras(notificationMessage); - JPushPlugin.notificationExtras = extras; - JPushPlugin.transmitNotificationReceive(title, alert, extras); + } } @Override @@ -126,15 +128,17 @@ public class JPushEventReceiver extends JPushMessageReceiver { // super.onNotifyMessageOpened(context, notificationMessage); cn.jiguang.cordova.push.JLogger.d(TAG,"onNotifyMessageOpened:"+notificationMessage); - String title = notificationMessage.notificationTitle; - JPushPlugin.openNotificationTitle = title; + try { + JSONObject jsonObject=new JSONObject(); + jsonObject.put("title", notificationMessage.notificationTitle); + jsonObject.put("alert", notificationMessage.notificationContent); + getExtras(jsonObject,notificationMessage); + JPushPlugin.openNotificationJson = jsonObject; + cn.jiguang.cordova.push.JPushPlugin.transmitNotificationOpen(jsonObject); + }catch (Throwable throwable){ + cn.jiguang.cordova.push.JLogger.d(TAG,"onNotifyMessageOpened throwable:"+throwable); - String alert = notificationMessage.notificationContent; - JPushPlugin.openNotificationAlert = alert; - - Map extras = getExtras(notificationMessage); - JPushPlugin.openNotificationExtras = extras; - JPushPlugin.transmitNotificationOpen(title, alert, extras); + } Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); if (launch != null) { launch.addCategory(Intent.CATEGORY_LAUNCHER); @@ -142,8 +146,7 @@ public class JPushEventReceiver extends JPushMessageReceiver { context.startActivity(launch); } } - private Map getExtras(NotificationMessage notificationMessage) { - Map extras = new HashMap<>(); + private void getExtras(JSONObject extras,NotificationMessage notificationMessage) { try { extras.put(JPushInterface.EXTRA_MSG_ID, notificationMessage.msgId); extras.put(JPushInterface.EXTRA_NOTIFICATION_ID, notificationMessage.notificationId); @@ -171,7 +174,6 @@ public class JPushEventReceiver extends JPushMessageReceiver { } catch (Throwable e) { Log.e(TAG, "[onNotifyMessageUnShow] e:" + e.getMessage()); } - return extras; } @Override public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) { diff --git a/src/android/JPushPlugin.java b/src/android/JPushPlugin.java index 61da417..0b62a17 100644 --- a/src/android/JPushPlugin.java +++ b/src/android/JPushPlugin.java @@ -42,12 +42,10 @@ public class JPushPlugin extends CordovaPlugin { private static JPushPlugin instance; private static Activity cordovaActivity; - static String notificationTitle; - static String notificationAlert; + static JSONObject notificationJson; static Map notificationExtras = new HashMap(); - static String openNotificationTitle; - static String openNotificationAlert; + static JSONObject openNotificationJson; static Map openNotificationExtras = new HashMap(); static Map eventCallbackMap = new HashMap(); @@ -68,22 +66,20 @@ public class JPushPlugin extends CordovaPlugin { // 如果同时缓存了打开事件 openNotificationAlert 和 消息事件 notificationAlert,只向 UI 发打开事件。 // 这样做是为了和 iOS 统一。 - if (openNotificationAlert != null) { - notificationAlert = null; - transmitNotificationOpen(openNotificationTitle, openNotificationAlert, openNotificationExtras); + if (openNotificationJson != null) { + transmitNotificationOpen(openNotificationJson); } - if (notificationAlert != null) { - transmitNotificationReceive(notificationTitle, notificationAlert, notificationExtras); + if (notificationJson != null) { + transmitNotificationReceive(notificationJson); } } public void onResume(boolean multitasking) { - if (openNotificationAlert != null) { - notificationAlert = null; - transmitNotificationOpen(openNotificationTitle, openNotificationAlert, openNotificationExtras); + if (openNotificationJson != null) { + transmitNotificationOpen(openNotificationJson); } - if (notificationAlert != null) { - transmitNotificationReceive(notificationTitle, notificationAlert, notificationExtras); + if (notificationJson != null) { + transmitNotificationReceive(notificationJson); } } @@ -161,11 +157,11 @@ public class JPushPlugin extends CordovaPlugin { return data; } - static void transmitMessageReceive(String message, Map extras) { + static void transmitMessageReceive(JSONObject data) { if (instance == null) { return; } - JSONObject data = getMessageObject(message, extras); +// JSONObject data = getMessageObject(message, extras); String format = "window.plugins.jPushPlugin.receiveMessageInAndroidCallback(%s);"; final String js = String.format(format, data.toString()); cordovaActivity.runOnUiThread(new Runnable() { @@ -203,11 +199,11 @@ public class JPushPlugin extends CordovaPlugin { } - static void transmitNotificationOpen(String title, String alert, Map extras) { + static void transmitNotificationOpen(JSONObject data) { if (instance == null) { return; } - JSONObject data = getNotificationObject(title, alert, extras); +// JSONObject data = getNotificationObject(title, alert, extras); String format = "window.plugins.jPushPlugin.openNotificationInAndroidCallback(%s);"; final String js = String.format(format, data.toString()); cordovaActivity.runOnUiThread(new Runnable() { @@ -216,15 +212,14 @@ public class JPushPlugin extends CordovaPlugin { instance.webView.loadUrl("javascript:" + js); } }); - JPushPlugin.openNotificationTitle = null; - JPushPlugin.openNotificationAlert = null; + JPushPlugin.openNotificationJson = null; } - static void transmitNotificationReceive(String title, String alert, Map extras) { + static void transmitNotificationReceive(JSONObject data) { if (instance == null) { return; } - JSONObject data = getNotificationObject(title, alert, extras); +// JSONObject data = getNotificationObject(title, alert, extras); String format = "window.plugins.jPushPlugin.receiveNotificationInAndroidCallback(%s);"; final String js = String.format(format, data.toString()); cordovaActivity.runOnUiThread(new Runnable() { @@ -233,8 +228,7 @@ public class JPushPlugin extends CordovaPlugin { instance.webView.loadUrl("javascript:" + js); } }); - JPushPlugin.notificationTitle = null; - JPushPlugin.notificationAlert = null; + JPushPlugin.notificationJson = null; } static void transmitReceiveRegistrationId(String rId) { diff --git a/src/android/JPushReceiver.java b/src/android/JPushReceiver.java index ced40a6..329bc83 100644 --- a/src/android/JPushReceiver.java +++ b/src/android/JPushReceiver.java @@ -31,56 +31,56 @@ public class JPushReceiver extends BroadcastReceiver { // } } - private void handlingMessageReceive(Intent intent) { - String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE); - Map extras = getNotificationExtras(intent); - JPushPlugin.transmitMessageReceive(msg, extras); - } - - private void handlingNotificationOpen(Context context, Intent intent) { - String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE); - JPushPlugin.openNotificationTitle = title; - - String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); - JPushPlugin.openNotificationAlert = alert; - - Map extras = getNotificationExtras(intent); - JPushPlugin.openNotificationExtras = extras; - - JPushPlugin.transmitNotificationOpen(title, alert, extras); - - Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); - if (launch != null) { - launch.addCategory(Intent.CATEGORY_LAUNCHER); - launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); - context.startActivity(launch); - } - } - - private void handlingNotificationReceive(Context context, Intent intent) { - String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE); - JPushPlugin.notificationTitle = title; - - String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); - JPushPlugin.notificationAlert = alert; - - Map extras = getNotificationExtras(intent); - JPushPlugin.notificationExtras = extras; - - JPushPlugin.transmitNotificationReceive(title, alert, extras); - } - - private Map getNotificationExtras(Intent intent) { - Map extrasMap = new HashMap(); - for (String key : intent.getExtras().keySet()) { - if (!IGNORED_EXTRAS_KEYS.contains(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 void handlingMessageReceive(Intent intent) { +// String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE); +// Map extras = getNotificationExtras(intent); +// JPushPlugin.transmitMessageReceive(msg, extras); +// } +// +// private void handlingNotificationOpen(Context context, Intent intent) { +// String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE); +// JPushPlugin.openNotificationJson = title; +// +// String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); +// JPushPlugin.openNotificationAlert = alert; +// +// Map extras = getNotificationExtras(intent); +// JPushPlugin.openNotificationExtras = extras; +// +// JPushPlugin.transmitNotificationOpen(title, alert, extras); +// +// Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); +// if (launch != null) { +// launch.addCategory(Intent.CATEGORY_LAUNCHER); +// launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); +// context.startActivity(launch); +// } +// } +// +// private void handlingNotificationReceive(Context context, Intent intent) { +// String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE); +// JPushPlugin.notificationTitle = title; +// +// String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); +// JPushPlugin.notificationAlert = alert; +// +// Map extras = getNotificationExtras(intent); +// JPushPlugin.notificationExtras = extras; +// +// JPushPlugin.transmitNotificationReceive(title, alert, extras); +// } +// +// private Map getNotificationExtras(Intent intent) { +// Map extrasMap = new HashMap(); +// for (String key : intent.getExtras().keySet()) { +// if (!IGNORED_EXTRAS_KEYS.contains(key)) { +// if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) { +// extrasMap.put(key, intent.getIntExtra(key, 0)); +// } else { +// extrasMap.put(key, intent.getStringExtra(key)); +// } +// } +// } +// return extrasMap; +// } }