mirror of
https://github.com/jpush/jpush-phonegap-plugin.git
synced 2025-02-22 14:02:55 +08:00
修复:鸿蒙设备上后台不能收到消息问题。全部采用JPushMessageReceiver进行回调。
This commit is contained in:
parent
13c59302f4
commit
0d64cb853b
@ -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);
|
||||
|
||||
JPushPlugin.transmitMessageReceive(customMessage.message, getExtras(customMessage));
|
||||
|
||||
// String msg = customMessage.message;//intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);
|
||||
// Map<String, Object> extras = getNotificationExtras(intent);
|
||||
// JPushPlugin.transmitMessageReceive(msg, extras);
|
||||
}
|
||||
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);
|
||||
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) {
|
||||
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
|
||||
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<String, Object> 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<String, Object> 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<String, Object> getExtras(NotificationMessage notificationMessage) {
|
||||
Map<String, Object> 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) {
|
||||
|
@ -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<String, Object> notificationExtras = new HashMap<String, Object>();
|
||||
|
||||
static String openNotificationTitle;
|
||||
static String openNotificationAlert;
|
||||
static JSONObject openNotificationJson;
|
||||
static Map<String, Object> openNotificationExtras = new HashMap<String, Object>();
|
||||
|
||||
static Map<Integer, CallbackContext> eventCallbackMap = new HashMap<Integer, CallbackContext>();
|
||||
@ -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<String, Object> 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<String, Object> 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<String, Object> 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) {
|
||||
|
@ -31,56 +31,56 @@ public class JPushReceiver extends BroadcastReceiver {
|
||||
// }
|
||||
}
|
||||
|
||||
private void handlingMessageReceive(Intent intent) {
|
||||
String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);
|
||||
Map<String, Object> 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<String, Object> 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<String, Object> extras = getNotificationExtras(intent);
|
||||
JPushPlugin.notificationExtras = extras;
|
||||
|
||||
JPushPlugin.transmitNotificationReceive(title, alert, extras);
|
||||
}
|
||||
|
||||
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)) {
|
||||
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<String, Object> 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<String, Object> 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<String, Object> extras = getNotificationExtras(intent);
|
||||
// JPushPlugin.notificationExtras = extras;
|
||||
//
|
||||
// JPushPlugin.transmitNotificationReceive(title, alert, extras);
|
||||
// }
|
||||
//
|
||||
// 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)) {
|
||||
// if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
|
||||
// extrasMap.put(key, intent.getIntExtra(key, 0));
|
||||
// } else {
|
||||
// extrasMap.put(key, intent.getStringExtra(key));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return extrasMap;
|
||||
// }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user