diff --git a/android/src/main/java/cn/jiguang/plugins/push/JPushModule.java b/android/src/main/java/cn/jiguang/plugins/push/JPushModule.java index 21ab0fcb..cc62195a 100644 --- a/android/src/main/java/cn/jiguang/plugins/push/JPushModule.java +++ b/android/src/main/java/cn/jiguang/plugins/push/JPushModule.java @@ -5,10 +5,10 @@ import android.app.Application; import android.os.Bundle; import android.text.TextUtils; -import android.util.Log; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; @@ -27,16 +27,20 @@ import cn.jiguang.plugins.push.receiver.JPushBroadcastReceiver; import cn.jpush.android.api.BasicPushNotificationBuilder; import cn.jpush.android.api.JPushInterface; +import cn.jpush.android.api.NotificationMessage; import cn.jpush.android.data.JPushLocalNotification; -public class JPushModule extends ReactContextBaseJavaModule { +public class JPushModule extends ReactContextBaseJavaModule implements LifecycleEventListener { public static ReactApplicationContext reactContext; public static boolean isAppForeground = false; + public static NotificationMessage notificationMessage = null; + public JPushModule(ReactApplicationContext reactApplicationContext) { super(reactContext); + reactApplicationContext.addLifecycleEventListener(this); reactContext = reactApplicationContext; } @@ -59,6 +63,11 @@ public void init() { JPushHelper.sendEvent(JConstants.NOTIFICATION_EVENT, writableMap); JPushBroadcastReceiver.NOTIFICATION_BUNDLE = null; } + if (JPushModule.notificationMessage != null) { + WritableMap writableMap = JPushHelper.convertNotificationToMap(JConstants.NOTIFICATION_OPENED, notificationMessage); + JPushHelper.sendEvent(JConstants.NOTIFICATION_EVENT, writableMap); + JPushModule.notificationMessage = null; + } } @ReactMethod @@ -371,7 +380,7 @@ public void addLocalNotification(ReadableMap readableMap) { return; } String notificationID = readableMap.getString(JConstants.MESSAGE_ID); - if(notificationID==null || TextUtils.isEmpty(notificationID)){ + if (notificationID == null || TextUtils.isEmpty(notificationID)) { JLogger.w(JConstants.PARAMS_ILLEGAL); return; } @@ -398,7 +407,7 @@ public void removeLocalNotification(ReadableMap readableMap) { } if (readableMap.hasKey(JConstants.MESSAGE_ID)) { String notificationID = readableMap.getString(JConstants.MESSAGE_ID); - if(notificationID==null || TextUtils.isEmpty(notificationID)){ + if (notificationID == null || TextUtils.isEmpty(notificationID)) { JLogger.w(JConstants.PARAMS_ILLEGAL); return; } @@ -462,33 +471,33 @@ public void deleteGeofence(ReadableMap readableMap) { } @ReactMethod - public void clearAllNotifications(){ + public void clearAllNotifications() { JPushInterface.clearAllNotifications(reactContext); } @ReactMethod - public void clearNotificationById(ReadableMap readableMap){ - if (readableMap == null){ + public void clearNotificationById(ReadableMap readableMap) { + if (readableMap == null) { JLogger.w(JConstants.PARAMS_NULL); return; } - if (readableMap.hasKey(JConstants.NOTIFICATION_ID)){ + if (readableMap.hasKey(JConstants.NOTIFICATION_ID)) { Integer id = readableMap.getInt(JConstants.NOTIFICATION_ID); - JPushInterface.clearNotificationById(reactContext,id); - }else { + JPushInterface.clearNotificationById(reactContext, id); + } else { JLogger.w("there are no " + JConstants.GEO_FENCE_ID); } } @ReactMethod - public void setPowerSaveMode(boolean bool){ - JPushInterface.setPowerSaveMode(reactContext,bool); + public void setPowerSaveMode(boolean bool) { + JPushInterface.setPowerSaveMode(reactContext, bool); } @ReactMethod - public void isNotificationEnabled(Callback callback){ + public void isNotificationEnabled(Callback callback) { Integer isEnabled = JPushInterface.isNotificationEnabled(reactContext); - if (callback == null){ + if (callback == null) { JLogger.w(JConstants.CALLBACK_NULL); return; } @@ -538,4 +547,26 @@ public void onActivityDestroyed(Activity activity) { }); } + @Override + public void onHostResume() { + + } + + @Override + public void onHostPause() { + + } + + @Override + public void onHostDestroy() { + JLogger.d("onHostDestroy"); + notificationMessage = null; + } + + @Override + public void onCatalystInstanceDestroy() { + super.onCatalystInstanceDestroy(); + JLogger.d("onCatalystInstanceDestroy"); + notificationMessage = null; + } } diff --git a/android/src/main/java/cn/jiguang/plugins/push/receiver/JPushModuleReceiver.java b/android/src/main/java/cn/jiguang/plugins/push/receiver/JPushModuleReceiver.java index 34ba7343..f7d65843 100644 --- a/android/src/main/java/cn/jiguang/plugins/push/receiver/JPushModuleReceiver.java +++ b/android/src/main/java/cn/jiguang/plugins/push/receiver/JPushModuleReceiver.java @@ -40,6 +40,7 @@ public void onNotifyMessageOpened(Context context, NotificationMessage notificat JLogger.d("onNotifyMessageOpened:" + notificationMessage.toString()); if (JPushModule.reactContext != null) { if (!JPushModule.isAppForeground) JPushHelper.launchApp(context); + JPushModule.notificationMessage = notificationMessage; WritableMap writableMap = JPushHelper.convertNotificationToMap(JConstants.NOTIFICATION_OPENED, notificationMessage); JPushHelper.sendEvent(JConstants.NOTIFICATION_EVENT, writableMap); } else { diff --git a/index.d.ts b/index.d.ts index a36e429f..9aed2d9e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -284,7 +284,7 @@ export default class JPush { /** * 自定义消息事件 */ - static addCustomMessagegListener( + static addCustomMessageListener( callback: Callback<{ /** * 唯一标识自定义消息的 ID diff --git a/index.js b/index.js index aa79258c..ebc46656 100644 --- a/index.js +++ b/index.js @@ -441,7 +441,7 @@ export default class JPush { * extras:对应 Portal 推送消息界面上的“可选设置”里的附加字段 * * */ - static addCustomMessagegListener(callback) { + static addCustomMessageListener(callback) { listeners[callback] = DeviceEventEmitter.addListener( CustomMessageEvent, result => { callback(result) diff --git a/package.json b/package.json index bb88b7e4..dc1dc6e1 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { - "name": "jpush-react-native", + "name": "react-native-mjpush", "description": "React Native JPush component for Android and iOS", - "homepage":"https://github.com/jpush/jpush-react-native", + "homepage":"https://github.com/bashen1/jpush-react-native", "main": "index.js", "types": "index.d.ts", "license": "ISC", "author": "wicked.tc130", - "version": "2.8.1", + "version": "2.8.2", "repository": { "type": "git", - "url": "https://github.com/jpush/jpush-react-native" + "url": "https://github.com/bashen1/jpush-react-native" }, "keywords": [ "react-native",