修复:鸿蒙设备上后台不能收到消息问题。全部采用JPushMessageReceiver进行回调。

This commit is contained in:
LcTerry 2025-02-12 11:59:32 +08:00
parent 13c59302f4
commit 0d64cb853b
3 changed files with 105 additions and 109 deletions

View File

@ -89,36 +89,38 @@ public class JPushEventReceiver extends JPushMessageReceiver {
public void onMessage(Context context, CustomMessage customMessage) { public void onMessage(Context context, CustomMessage customMessage) {
// super.onMessage(context,customMessage); // super.onMessage(context,customMessage);
cn.jiguang.cordova.push.JLogger.d(TAG,"onMessage:"+customMessage); cn.jiguang.cordova.push.JLogger.d(TAG,"onMessage:"+customMessage);
try {
JPushPlugin.transmitMessageReceive(customMessage.message, getExtras(customMessage)); JSONObject jsonObject=new JSONObject();
jsonObject.put("message", customMessage.message);
// String msg = customMessage.message;//intent.getStringExtra(JPushInterface.EXTRA_MESSAGE); jsonObject.put("alert", customMessage.title);
// Map<String, Object> extras = getNotificationExtras(intent); jsonObject.put(JPushInterface.EXTRA_EXTRA, stringToMap(customMessage.extra));
// JPushPlugin.transmitMessageReceive(msg, extras); jsonObject.put(JPushInterface.EXTRA_MSG_ID, customMessage.messageId);
} jsonObject.put(JPushInterface.EXTRA_CONTENT_TYPE, customMessage.contentType);
private Map<String, Object> getExtras(CustomMessage customMessage) {
Map<String, Object> 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) { if (JPushConstants.SDK_VERSION_CODE >= 387) {
extra.put(JPushInterface.EXTRA_TYPE_PLATFORM, customMessage.platform); jsonObject.put(JPushInterface.EXTRA_TYPE_PLATFORM, customMessage.platform);
} }
return extra; cn.jiguang.cordova.push.JPushPlugin.transmitNotificationReceive(jsonObject);
}catch (Throwable throwable){
cn.jiguang.cordova.push.JLogger.d(TAG,"onMessage throwable:"+throwable);
} }
}
@Override @Override
public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) { public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
// super.onNotifyMessageArrived(context, notificationMessage); // super.onNotifyMessageArrived(context, notificationMessage);
cn.jiguang.cordova.push.JLogger.d(TAG,"onNotifyMessageArrived:"+notificationMessage); cn.jiguang.cordova.push.JLogger.d(TAG,"onNotifyMessageArrived:"+notificationMessage);
String title = notificationMessage.notificationTitle; try {
JPushPlugin.notificationTitle = title; 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<String, Object> extras = getExtras(notificationMessage);
JPushPlugin.notificationExtras = extras;
JPushPlugin.transmitNotificationReceive(title, alert, extras);
} }
@Override @Override
@ -126,15 +128,17 @@ public class JPushEventReceiver extends JPushMessageReceiver {
// super.onNotifyMessageOpened(context, notificationMessage); // super.onNotifyMessageOpened(context, notificationMessage);
cn.jiguang.cordova.push.JLogger.d(TAG,"onNotifyMessageOpened:"+notificationMessage); cn.jiguang.cordova.push.JLogger.d(TAG,"onNotifyMessageOpened:"+notificationMessage);
String title = notificationMessage.notificationTitle; try {
JPushPlugin.openNotificationTitle = title; 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<String, Object> extras = getExtras(notificationMessage);
JPushPlugin.openNotificationExtras = extras;
JPushPlugin.transmitNotificationOpen(title, alert, extras);
Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if (launch != null) { if (launch != null) {
launch.addCategory(Intent.CATEGORY_LAUNCHER); launch.addCategory(Intent.CATEGORY_LAUNCHER);
@ -142,8 +146,7 @@ public class JPushEventReceiver extends JPushMessageReceiver {
context.startActivity(launch); context.startActivity(launch);
} }
} }
private Map<String, Object> getExtras(NotificationMessage notificationMessage) { private void getExtras(JSONObject extras,NotificationMessage notificationMessage) {
Map<String, Object> extras = new HashMap<>();
try { try {
extras.put(JPushInterface.EXTRA_MSG_ID, notificationMessage.msgId); extras.put(JPushInterface.EXTRA_MSG_ID, notificationMessage.msgId);
extras.put(JPushInterface.EXTRA_NOTIFICATION_ID, notificationMessage.notificationId); extras.put(JPushInterface.EXTRA_NOTIFICATION_ID, notificationMessage.notificationId);
@ -171,7 +174,6 @@ public class JPushEventReceiver extends JPushMessageReceiver {
} catch (Throwable e) { } catch (Throwable e) {
Log.e(TAG, "[onNotifyMessageUnShow] e:" + e.getMessage()); Log.e(TAG, "[onNotifyMessageUnShow] e:" + e.getMessage());
} }
return extras;
} }
@Override @Override
public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) { public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {

View File

@ -42,12 +42,10 @@ public class JPushPlugin extends CordovaPlugin {
private static JPushPlugin instance; private static JPushPlugin instance;
private static Activity cordovaActivity; private static Activity cordovaActivity;
static String notificationTitle; static JSONObject notificationJson;
static String notificationAlert;
static Map<String, Object> notificationExtras = new HashMap<String, Object>(); static Map<String, Object> notificationExtras = new HashMap<String, Object>();
static String openNotificationTitle; static JSONObject openNotificationJson;
static String openNotificationAlert;
static Map<String, Object> openNotificationExtras = new HashMap<String, Object>(); static Map<String, Object> openNotificationExtras = new HashMap<String, Object>();
static Map<Integer, CallbackContext> eventCallbackMap = new HashMap<Integer, CallbackContext>(); static Map<Integer, CallbackContext> eventCallbackMap = new HashMap<Integer, CallbackContext>();
@ -68,22 +66,20 @@ public class JPushPlugin extends CordovaPlugin {
// 如果同时缓存了打开事件 openNotificationAlert 消息事件 notificationAlert只向 UI 发打开事件 // 如果同时缓存了打开事件 openNotificationAlert 消息事件 notificationAlert只向 UI 发打开事件
// 这样做是为了和 iOS 统一 // 这样做是为了和 iOS 统一
if (openNotificationAlert != null) { if (openNotificationJson != null) {
notificationAlert = null; transmitNotificationOpen(openNotificationJson);
transmitNotificationOpen(openNotificationTitle, openNotificationAlert, openNotificationExtras);
} }
if (notificationAlert != null) { if (notificationJson != null) {
transmitNotificationReceive(notificationTitle, notificationAlert, notificationExtras); transmitNotificationReceive(notificationJson);
} }
} }
public void onResume(boolean multitasking) { public void onResume(boolean multitasking) {
if (openNotificationAlert != null) { if (openNotificationJson != null) {
notificationAlert = null; transmitNotificationOpen(openNotificationJson);
transmitNotificationOpen(openNotificationTitle, openNotificationAlert, openNotificationExtras);
} }
if (notificationAlert != null) { if (notificationJson != null) {
transmitNotificationReceive(notificationTitle, notificationAlert, notificationExtras); transmitNotificationReceive(notificationJson);
} }
} }
@ -161,11 +157,11 @@ public class JPushPlugin extends CordovaPlugin {
return data; return data;
} }
static void transmitMessageReceive(String message, Map<String, Object> extras) { static void transmitMessageReceive(JSONObject data) {
if (instance == null) { if (instance == null) {
return; return;
} }
JSONObject data = getMessageObject(message, extras); // JSONObject data = getMessageObject(message, extras);
String format = "window.plugins.jPushPlugin.receiveMessageInAndroidCallback(%s);"; String format = "window.plugins.jPushPlugin.receiveMessageInAndroidCallback(%s);";
final String js = String.format(format, data.toString()); final String js = String.format(format, data.toString());
cordovaActivity.runOnUiThread(new Runnable() { cordovaActivity.runOnUiThread(new Runnable() {
@ -203,11 +199,11 @@ public class JPushPlugin extends CordovaPlugin {
} }
static void transmitNotificationOpen(String title, String alert, Map<String, Object> extras) { static void transmitNotificationOpen(JSONObject data) {
if (instance == null) { if (instance == null) {
return; return;
} }
JSONObject data = getNotificationObject(title, alert, extras); // JSONObject data = getNotificationObject(title, alert, extras);
String format = "window.plugins.jPushPlugin.openNotificationInAndroidCallback(%s);"; String format = "window.plugins.jPushPlugin.openNotificationInAndroidCallback(%s);";
final String js = String.format(format, data.toString()); final String js = String.format(format, data.toString());
cordovaActivity.runOnUiThread(new Runnable() { cordovaActivity.runOnUiThread(new Runnable() {
@ -216,15 +212,14 @@ public class JPushPlugin extends CordovaPlugin {
instance.webView.loadUrl("javascript:" + js); instance.webView.loadUrl("javascript:" + js);
} }
}); });
JPushPlugin.openNotificationTitle = null; JPushPlugin.openNotificationJson = null;
JPushPlugin.openNotificationAlert = null;
} }
static void transmitNotificationReceive(String title, String alert, Map<String, Object> extras) { static void transmitNotificationReceive(JSONObject data) {
if (instance == null) { if (instance == null) {
return; return;
} }
JSONObject data = getNotificationObject(title, alert, extras); // JSONObject data = getNotificationObject(title, alert, extras);
String format = "window.plugins.jPushPlugin.receiveNotificationInAndroidCallback(%s);"; String format = "window.plugins.jPushPlugin.receiveNotificationInAndroidCallback(%s);";
final String js = String.format(format, data.toString()); final String js = String.format(format, data.toString());
cordovaActivity.runOnUiThread(new Runnable() { cordovaActivity.runOnUiThread(new Runnable() {
@ -233,8 +228,7 @@ public class JPushPlugin extends CordovaPlugin {
instance.webView.loadUrl("javascript:" + js); instance.webView.loadUrl("javascript:" + js);
} }
}); });
JPushPlugin.notificationTitle = null; JPushPlugin.notificationJson = null;
JPushPlugin.notificationAlert = null;
} }
static void transmitReceiveRegistrationId(String rId) { static void transmitReceiveRegistrationId(String rId) {

View File

@ -31,56 +31,56 @@ public class JPushReceiver extends BroadcastReceiver {
// } // }
} }
private void handlingMessageReceive(Intent intent) { // private void handlingMessageReceive(Intent intent) {
String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE); // String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);
Map<String, Object> extras = getNotificationExtras(intent); // Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.transmitMessageReceive(msg, extras); // JPushPlugin.transmitMessageReceive(msg, extras);
} // }
//
private void handlingNotificationOpen(Context context, Intent intent) { // private void handlingNotificationOpen(Context context, Intent intent) {
String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE); // String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
JPushPlugin.openNotificationTitle = title; // JPushPlugin.openNotificationJson = title;
//
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); // String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
JPushPlugin.openNotificationAlert = alert; // JPushPlugin.openNotificationAlert = alert;
//
Map<String, Object> extras = getNotificationExtras(intent); // Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.openNotificationExtras = extras; // JPushPlugin.openNotificationExtras = extras;
//
JPushPlugin.transmitNotificationOpen(title, alert, extras); // JPushPlugin.transmitNotificationOpen(title, alert, extras);
//
Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); // Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if (launch != null) { // if (launch != null) {
launch.addCategory(Intent.CATEGORY_LAUNCHER); // launch.addCategory(Intent.CATEGORY_LAUNCHER);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); // launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(launch); // context.startActivity(launch);
} // }
} // }
//
private void handlingNotificationReceive(Context context, Intent intent) { // private void handlingNotificationReceive(Context context, Intent intent) {
String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE); // String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
JPushPlugin.notificationTitle = title; // JPushPlugin.notificationTitle = title;
//
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); // String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
JPushPlugin.notificationAlert = alert; // JPushPlugin.notificationAlert = alert;
//
Map<String, Object> extras = getNotificationExtras(intent); // Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.notificationExtras = extras; // JPushPlugin.notificationExtras = extras;
//
JPushPlugin.transmitNotificationReceive(title, alert, extras); // JPushPlugin.transmitNotificationReceive(title, alert, extras);
} // }
//
private Map<String, Object> getNotificationExtras(Intent intent) { // private Map<String, Object> getNotificationExtras(Intent intent) {
Map<String, Object> extrasMap = new HashMap<String, Object>(); // 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)) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) { // if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
extrasMap.put(key, intent.getIntExtra(key, 0)); // extrasMap.put(key, intent.getIntExtra(key, 0));
} else { // } else {
extrasMap.put(key, intent.getStringExtra(key)); // extrasMap.put(key, intent.getStringExtra(key));
} // }
} // }
} // }
return extrasMap; // return extrasMap;
} // }
} }