Skip to content

Commit a85d83a

Browse files
authored
Merge pull request #509 from JoshLipan/master
Separate android caching.
2 parents d8f4140 + 39cc892 commit a85d83a

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ public class JPushModule extends ReactContextBaseJavaModule {
4444
private static String TAG = "JPushModule";
4545
private Context mContext;
4646
private static String mEvent;
47+
private static String mRidEvent;
48+
private static String mConnectEvent;
4749
private static Bundle mCachedBundle;
50+
private static Bundle mRidBundle;
51+
private static Bundle mConnectCachedBundle;
4852
private static ReactApplicationContext mRAC;
4953

5054
private final static String RECEIVE_NOTIFICATION = "receiveNotification";
@@ -80,10 +84,14 @@ public void initialize() {
8084
public void onCatalystInstanceDestroy() {
8185
super.onCatalystInstanceDestroy();
8286
mCachedBundle = null;
87+
mRidBundle = null;
88+
mConnectCachedBundle = null;
8389
if (null != sCacheMap) {
8490
sCacheMap.clear();
8591
}
8692
mEvent = null;
93+
mRidEvent = null;
94+
mConnectEvent = null;
8795
mGetRidCallback = null;
8896
}
8997

@@ -159,14 +167,6 @@ private static void sendEvent() {
159167
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
160168
.emit(mEvent, map);
161169
break;
162-
case RECEIVE_REGISTRATION_ID:
163-
if (mGetRidCallback != null) {
164-
mGetRidCallback.invoke(mCachedBundle.getString(JPushInterface.EXTRA_REGISTRATION_ID));
165-
mGetRidCallback = null;
166-
}
167-
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
168-
.emit(mEvent, mCachedBundle.getString(JPushInterface.EXTRA_REGISTRATION_ID));
169-
break;
170170
case RECEIVE_NOTIFICATION:
171171
case OPEN_NOTIFICATION:
172172
map = Arguments.createMap();
@@ -176,16 +176,33 @@ private static void sendEvent() {
176176
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
177177
.emit(mEvent, map);
178178
break;
179-
case CONNECTION_CHANGE:
180-
if (mCachedBundle != null) {
181-
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
182-
.emit(mEvent, mCachedBundle.getBoolean(JPushInterface.EXTRA_CONNECTION_CHANGE, false));
183-
}
184-
break;
185179
}
180+
186181
mEvent = null;
187182
mCachedBundle = null;
188183
}
184+
185+
if (mRidEvent != null) {
186+
Logger.i(TAG, "Sending ridevent : " + mRidEvent);
187+
if (mGetRidCallback != null) {
188+
mGetRidCallback.invoke(mRidBundle.getString(JPushInterface.EXTRA_REGISTRATION_ID));
189+
mGetRidCallback = null;
190+
}
191+
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
192+
.emit(mRidEvent, mRidBundle.getString(JPushInterface.EXTRA_REGISTRATION_ID));
193+
mRidEvent = null;
194+
mRidBundle = null;
195+
}
196+
197+
if (mConnectEvent != null) {
198+
Logger.i(TAG, "Sending connectevent : " + mConnectEvent);
199+
if (mConnectCachedBundle != null) {
200+
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(mConnectEvent,
201+
mConnectCachedBundle.getBoolean(JPushInterface.EXTRA_CONNECTION_CHANGE, false));
202+
}
203+
mConnectEvent = null;
204+
mConnectCachedBundle = null;
205+
}
189206
}
190207

191208
/**
@@ -502,8 +519,9 @@ public JPushReceiver() {
502519

503520
@Override
504521
public void onReceive(Context context, Intent data) {
505-
mCachedBundle = data.getExtras();
522+
506523
if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(data.getAction())) {
524+
mCachedBundle = data.getExtras();
507525
try {
508526
String message = data.getStringExtra(JPushInterface.EXTRA_MESSAGE);
509527
Logger.i(TAG, "收到自定义消息: " + message);
@@ -515,6 +533,7 @@ public void onReceive(Context context, Intent data) {
515533
e.printStackTrace();
516534
}
517535
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(data.getAction())) {
536+
mCachedBundle = data.getExtras();
518537
try {
519538
// 通知内容
520539
String alertContent = mCachedBundle.getString(JPushInterface.EXTRA_ALERT);
@@ -530,21 +549,15 @@ public void onReceive(Context context, Intent data) {
530549
e.printStackTrace();
531550
}
532551
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(data.getAction())) {
552+
mCachedBundle = data.getExtras();
533553
try {
534554
Logger.d(TAG, "用户点击打开了通知");
535555
// 通知内容
536556
String alertContent = mCachedBundle.getString(JPushInterface.EXTRA_ALERT);
537557
// extra 字段的 json 字符串
538558
String extras = mCachedBundle.getString(JPushInterface.EXTRA_EXTRA);
539-
Intent intent;
540-
// if (isApplicationRunningBackground(context)) {
541-
// intent = new Intent();
542-
// intent.setClassName(context.getPackageName(), context.getPackageName() + ".MainActivity");
543-
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
544-
// } else {
545-
intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
546-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
547-
// }
559+
Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
560+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
548561
intent.putExtras(mCachedBundle);
549562
context.startActivity(intent);
550563
mEvent = OPEN_NOTIFICATION;
@@ -559,17 +572,19 @@ public void onReceive(Context context, Intent data) {
559572
// After JPush finished registering, will send this broadcast, use JPushModule.addGetRegistrationIdListener
560573
// to get registrationId in the first instance.
561574
} else if (JPushInterface.ACTION_REGISTRATION_ID.equals(data.getAction())) {
575+
mRidBundle = data.getExtras();
562576
try {
563-
mEvent = RECEIVE_REGISTRATION_ID;
577+
mRidEvent = RECEIVE_REGISTRATION_ID;
564578
if (mRAC != null) {
565579
sendEvent();
566580
}
567581
} catch (Exception e) {
568582
e.printStackTrace();
569583
}
570584
} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(data.getAction())) {
585+
mConnectCachedBundle = data.getExtras();
571586
try {
572-
mEvent = CONNECTION_CHANGE;
587+
mConnectEvent = CONNECTION_CHANGE;
573588
if (mRAC != null) {
574589
sendEvent();
575590
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jpush-react-native",
3-
"version": "2.2.4",
3+
"version": "2.2.5",
44
"description": "a jpush plugin for react native application",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)