Skip to content

Commit

Permalink
Merge pull request #210 from jpush/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
KenChoi1992 authored May 3, 2017
2 parents d5ac956 + 07b087d commit f8ec3b5
Show file tree
Hide file tree
Showing 11 changed files with 562 additions and 53 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ dependencies {
- [Android API](https://github.com/jpush/jpush-react-native/blob/master/example/documents/Android%20API.md)
- [iOS API](https://github.com/jpush/jpush-react-native/blob/master/example/documents/iOS_API.md)

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


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

Expand Down
67 changes: 44 additions & 23 deletions android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ public void setStyleCustom() {
Logger.toast(mContext, "Custom Builder - 2");
}

/**
* Get registration id, different from JPushModule.addGetRegistrationIdListener, this
* method has no calling limits.
*
* @param callback callback with registrationId
*/
@ReactMethod
public void getRegistrationID(Callback callback) {
try {
Expand All @@ -229,11 +235,19 @@ public void getRegistrationID(Callback callback) {
}
}

/**
* Clear all notifications, suggest invoke this method while exiting app.
*/
@ReactMethod
public void clearAllNotifications() {
JPushInterface.clearAllNotifications(getReactApplicationContext());
}

/**
* Clear specified notification
*
* @param id the notification id
*/
@ReactMethod
public void clearNotificationById(String id) {
try {
Expand Down Expand Up @@ -295,45 +309,52 @@ public void onReceive(Context context, Intent data) {
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(data.getAction())) {
try {
Logger.d(TAG, "用户点击打开了通知");
if (mRAC == null) {
Log.e(TAG, "mRAC is null!");
mModule = new JPushModule((ReactApplicationContext) context);
Log.d(TAG, "mRAC is null ? " + (mRAC == null));
}
// 通知内容
String alertContent = bundle.getString(JPushInterface.EXTRA_ALERT);
// extra 字段的 json 字符串
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
WritableMap map = Arguments.createMap();
map.putString("alertContent", alertContent);
map.putString("extras", extras);
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("openNotification", map);
map.putString("jumpTo", "second");
// judge if application is running in background, opening initial Activity.
// You can change here to open appointed Activity. All you need to do is create
// the appointed Activity, and use JS render the appointed Activity.
// Please reference examples' SecondActivity for detail,
// and JS files are in folder: example/react-native-android
if (isApplicationRunningBackground(context)) {
Intent intent = new Intent();
intent.setClass(context, mModule.mContext.getClass());
Logger.d(TAG, "context.getClass: " + mModule.mContext.getClass());
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
// application running in foreground, do nothing
Intent intent = new Intent();
intent.setClassName(context.getPackageName(), context.getPackageName() + ".MainActivity");
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
// 如果需要跳转到指定的界面,那么需要同时启动 MainActivity 及指定界面:
// If you need to open appointed Activity, you need to start MainActivity and
// appointed Activity at the same time.
// Intent detailIntent = new Intent();
// detailIntent.setClassName(context.getPackageName(), context.getPackageName() + ".SecondActivity");
// detailIntent.putExtras(bundle);
// Intent[] intents = {intent, detailIntent};
// 同时启动 MainActivity 以及 SecondActivity
// context.startActivities(intents);
if (mRAC != null) {
Log.e(TAG, "Passing openNotification event");
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("openNotification", map);
} else {
Log.e(TAG, "mRAC still null!");
}
} catch (Exception e) {
e.printStackTrace();
Logger.i(TAG, "Try to start application");
try {
Intent intent = new Intent();
intent.setClassName(context.getPackageName(), context.getPackageName() + ".MainActivity");
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
} catch (Exception e1) {
e1.printStackTrace();
Logger.i(TAG, "Cannot find MainActivity, will discard onClick event.");
}
Logger.i(TAG, "Shouldn't access here");
}

// 应用注册完成后会发送广播,在 JS 中 JPushModule.addGetRegistrationIdListener 接口可以第一时间得到 registrationId
// After JPush finished registering, will send this broadcast, use JPushModule.addGetRegistrationIdListener
// to get registrationId in the first instance.
} else if (JPushInterface.ACTION_REGISTRATION_ID.equals(data.getAction())) {
String registrationId = data.getExtras().getString(JPushInterface.EXTRA_REGISTRATION_ID);
Logger.d(TAG, "注册成功, registrationId: " + registrationId);
Expand Down
Loading

0 comments on commit f8ec3b5

Please sign in to comment.