Skip to content

Commit f8ec3b5

Browse files
authored
Merge pull request #210 from jpush/dev
Dev
2 parents d5ac956 + 07b087d commit f8ec3b5

File tree

11 files changed

+562
-53
lines changed

11 files changed

+562
-53
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ dependencies {
8484
- [Android API](https://github.com/jpush/jpush-react-native/blob/master/example/documents/Android%20API.md)
8585
- [iOS API](https://github.com/jpush/jpush-react-native/blob/master/example/documents/iOS_API.md)
8686

87+
### 关于点击通知跳转到指定界面
88+
demo 增加了 [JPushModuleDemo](./example/android/app/src/com/pushdemo/JPushModuleDemo.java) 类(其中点击通知的地方做了一下跳转),
89+
可以在应用处于前台,后台或者为启动状态时,点击通知跳转到指定界面(SecondActivity)。
90+
对应的 JS 文件为 [second.js](./example/react-native-android/second.js),详情可以参考这两个文件。
91+
8792

8893
### [关于更新 RN](https://github.com/jpush/jpush-react-native/blob/master/example/documents/Update%20React%20Native.md)
8994

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

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ public void setStyleCustom() {
218218
Logger.toast(mContext, "Custom Builder - 2");
219219
}
220220

221+
/**
222+
* Get registration id, different from JPushModule.addGetRegistrationIdListener, this
223+
* method has no calling limits.
224+
*
225+
* @param callback callback with registrationId
226+
*/
221227
@ReactMethod
222228
public void getRegistrationID(Callback callback) {
223229
try {
@@ -229,11 +235,19 @@ public void getRegistrationID(Callback callback) {
229235
}
230236
}
231237

238+
/**
239+
* Clear all notifications, suggest invoke this method while exiting app.
240+
*/
232241
@ReactMethod
233242
public void clearAllNotifications() {
234243
JPushInterface.clearAllNotifications(getReactApplicationContext());
235244
}
236245

246+
/**
247+
* Clear specified notification
248+
*
249+
* @param id the notification id
250+
*/
237251
@ReactMethod
238252
public void clearNotificationById(String id) {
239253
try {
@@ -295,45 +309,52 @@ public void onReceive(Context context, Intent data) {
295309
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(data.getAction())) {
296310
try {
297311
Logger.d(TAG, "用户点击打开了通知");
312+
if (mRAC == null) {
313+
Log.e(TAG, "mRAC is null!");
314+
mModule = new JPushModule((ReactApplicationContext) context);
315+
Log.d(TAG, "mRAC is null ? " + (mRAC == null));
316+
}
298317
// 通知内容
299318
String alertContent = bundle.getString(JPushInterface.EXTRA_ALERT);
300319
// extra 字段的 json 字符串
301320
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
302321
WritableMap map = Arguments.createMap();
303322
map.putString("alertContent", alertContent);
304323
map.putString("extras", extras);
305-
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
306-
.emit("openNotification", map);
324+
map.putString("jumpTo", "second");
307325
// judge if application is running in background, opening initial Activity.
308326
// You can change here to open appointed Activity. All you need to do is create
309327
// the appointed Activity, and use JS render the appointed Activity.
310328
// Please reference examples' SecondActivity for detail,
311329
// and JS files are in folder: example/react-native-android
312-
if (isApplicationRunningBackground(context)) {
313-
Intent intent = new Intent();
314-
intent.setClass(context, mModule.mContext.getClass());
315-
Logger.d(TAG, "context.getClass: " + mModule.mContext.getClass());
316-
intent.putExtras(bundle);
317-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
318-
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
319-
context.startActivity(intent);
320-
// application running in foreground, do nothing
330+
Intent intent = new Intent();
331+
intent.setClassName(context.getPackageName(), context.getPackageName() + ".MainActivity");
332+
intent.putExtras(bundle);
333+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
334+
context.startActivity(intent);
335+
// 如果需要跳转到指定的界面,那么需要同时启动 MainActivity 及指定界面:
336+
// If you need to open appointed Activity, you need to start MainActivity and
337+
// appointed Activity at the same time.
338+
// Intent detailIntent = new Intent();
339+
// detailIntent.setClassName(context.getPackageName(), context.getPackageName() + ".SecondActivity");
340+
// detailIntent.putExtras(bundle);
341+
// Intent[] intents = {intent, detailIntent};
342+
// 同时启动 MainActivity 以及 SecondActivity
343+
// context.startActivities(intents);
344+
if (mRAC != null) {
345+
Log.e(TAG, "Passing openNotification event");
346+
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
347+
.emit("openNotification", map);
348+
} else {
349+
Log.e(TAG, "mRAC still null!");
321350
}
322351
} catch (Exception e) {
323352
e.printStackTrace();
324-
Logger.i(TAG, "Try to start application");
325-
try {
326-
Intent intent = new Intent();
327-
intent.setClassName(context.getPackageName(), context.getPackageName() + ".MainActivity");
328-
intent.putExtras(bundle);
329-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
330-
context.startActivity(intent);
331-
} catch (Exception e1) {
332-
e1.printStackTrace();
333-
Logger.i(TAG, "Cannot find MainActivity, will discard onClick event.");
334-
}
353+
Logger.i(TAG, "Shouldn't access here");
335354
}
336-
355+
// 应用注册完成后会发送广播,在 JS 中 JPushModule.addGetRegistrationIdListener 接口可以第一时间得到 registrationId
356+
// After JPush finished registering, will send this broadcast, use JPushModule.addGetRegistrationIdListener
357+
// to get registrationId in the first instance.
337358
} else if (JPushInterface.ACTION_REGISTRATION_ID.equals(data.getAction())) {
338359
String registrationId = data.getExtras().getString(JPushInterface.EXTRA_REGISTRATION_ID);
339360
Logger.d(TAG, "注册成功, registrationId: " + registrationId);

0 commit comments

Comments
 (0)