Skip to content

Commit 7198491

Browse files
authored
Merge pull request #310 from jpush/dev
add new API
2 parents 11c7103 + 904ff7c commit 7198491

File tree

13 files changed

+196
-133
lines changed

13 files changed

+196
-133
lines changed

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1818
import com.facebook.react.bridge.ReactMethod;
1919
import com.facebook.react.bridge.ReadableArray;
20+
import com.facebook.react.bridge.ReadableMap;
2021
import com.facebook.react.bridge.WritableArray;
2122
import com.facebook.react.bridge.WritableMap;
2223
import com.facebook.react.modules.core.DeviceEventManagerModule;
@@ -121,6 +122,16 @@ public void resumePush() {
121122
Logger.toast(mContext, "Resume push success");
122123
}
123124

125+
@ReactMethod
126+
public void crashLogOFF() {
127+
JPushInterface.stopCrashHandler(getReactApplicationContext());
128+
}
129+
130+
@ReactMethod
131+
public void crashLogON() {
132+
JPushInterface.initCrashHandler(getReactApplicationContext());
133+
}
134+
124135
@ReactMethod
125136
public void notifyJSDidLoad(Callback callback) {
126137
// send cached event
@@ -157,8 +168,10 @@ private static void sendEvent() {
157168
.emit(mEvent, map);
158169
break;
159170
case CONNECTION_CHANGE:
160-
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
161-
.emit(mEvent, mCachedBundle.getBoolean(JPushInterface.EXTRA_CONNECTION_CHANGE, false));
171+
if (mCachedBundle != null) {
172+
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
173+
.emit(mEvent, mCachedBundle.getBoolean(JPushInterface.EXTRA_CONNECTION_CHANGE, false));
174+
}
162175
break;
163176
}
164177
mEvent = null;
@@ -446,7 +459,7 @@ public void onReceive(Context context, Intent data) {
446459
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
447460
} else {
448461
intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
449-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
462+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
450463
}
451464
intent.putExtras(mCachedBundle);
452465
context.startActivity(intent);
@@ -575,6 +588,26 @@ public void jumpToPushActivity(String activityName) {
575588

576589
}
577590

591+
@ReactMethod
592+
public void jumpToPushActivityWithParams(String activityName, ReadableMap map) {
593+
Logger.d(TAG, "Jumping to " + activityName);
594+
try {
595+
Intent intent = new Intent();
596+
if (null != map) {
597+
while (map.keySetIterator().hasNextKey()) {
598+
String key = map.keySetIterator().nextKey();
599+
String value = map.getString(key);
600+
intent.putExtra(key, value);
601+
}
602+
}
603+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
604+
intent.setClassName(mRAC, mRAC.getPackageName() + "." + activityName);
605+
mRAC.startActivity(intent);
606+
} catch (Exception e) {
607+
e.printStackTrace();
608+
}
609+
}
610+
578611
@ReactMethod
579612
public void finishActivity() {
580613
try {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.jpush.reactnativejpush;
22

33
import com.facebook.react.ReactPackage;
4+
import com.facebook.react.bridge.JavaScriptModule;
45
import com.facebook.react.bridge.NativeModule;
56
import com.facebook.react.bridge.ReactApplicationContext;
67
import com.facebook.react.uimanager.ViewManager;
@@ -23,6 +24,11 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
2324
});
2425
}
2526

27+
// RN 0.47 remove this method
28+
public List<Class<? extends JavaScriptModule>> createJSModules() {
29+
return Collections.emptyList();
30+
}
31+
2632
@Override
2733
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
2834
return Collections.emptyList();

example/android/app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ android {
142142

143143
dependencies {
144144
compile fileTree(dir: "libs", include: ["*.jar"])
145-
compile project(':jpush-react-native')
146-
compile project(':jcore-react-native')
145+
api project(':jpush-react-native')
146+
api project(':jcore-react-native')
147147
compile "com.android.support:appcompat-v7:25.3.1"
148-
compile "com.facebook.react:react-native:0.47.1" // From node_modules
148+
compile "com.facebook.react:react-native:+" // From node_modules
149149
}
150150

151151
// Run this once to be able to run the application with BUCK

example/android/app/src/com/pushdemo/SecondActivity.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
package com.pushdemo;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
5+
import android.util.Log;
46

57
import com.facebook.react.ReactActivity;
8+
import com.facebook.react.bridge.Arguments;
9+
import com.facebook.react.bridge.WritableMap;
10+
import com.facebook.react.modules.core.DeviceEventManagerModule;
611

712
public class SecondActivity extends ReactActivity {
813

14+
private final static String KEY = "hello";
15+
private final static String RECEIVE_EXTRA_EVENT = "receiveExtras";
16+
917
@Override
1018
protected void onCreate(Bundle savedInstanceState) {
1119
super.onCreate(savedInstanceState);
20+
Intent intent = getIntent();
21+
if (null != intent) {
22+
String value = intent.getStringExtra("key");
23+
Log.i("SecondActivity", "Got intent, key: " + KEY + " value: " + value);
24+
WritableMap map = Arguments.createMap();
25+
map.putString(KEY, value);
26+
getReactInstanceManager().getCurrentReactContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
27+
.emit(RECEIVE_EXTRA_EVENT, map);
28+
}
1229
}
1330

1431
@Override

example/index.android.js

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,22 @@ import SecondActivity from './react-native-android/second';
1212
const {
1313
AppRegistry,
1414
BackAndroid,
15-
Navigator,
16-
1715
} = ReactNative;
16+
import {
17+
StackNavigator
18+
} from 'react-navigation';
1819
import JPushModule from 'jpush-react-native';
1920

20-
var navigator;
21-
class PushDemoApp extends React.Component {
22-
23-
constructor(props) {
24-
super(props);
25-
}
26-
27-
componentDidMount() {
28-
console.log("index did start!");
29-
var navigator = this.navigator;
30-
BackAndroid.addEventListener('hardwareBackPress', function() {
31-
if (navigator && navigator.getCurrentRoutes().length > 1) {
32-
navigator.pop();
33-
return true;
34-
}
35-
return false;
36-
});
37-
}
38-
39-
componentWillUnmount() {
40-
BackAndroid.removeEventListener('hardwareBackPress');
41-
}
42-
43-
configureScene(route) {
44-
return Navigator.SceneConfigs.FadeAndroid;
45-
}
46-
47-
renderScene(router, navigator) {
48-
var Component = null;
49-
this.navigator = navigator;
50-
switch (router.name) {
51-
case "pushActivity":
52-
Component = PushActivity;
53-
break;
54-
case "setActivity":
55-
Component = SetActivity;
56-
break;
57-
case "second":
58-
Component = SecondActivity;
59-
break;
60-
}
61-
62-
return <Component navigator = { navigator } />
63-
}
64-
65-
66-
67-
render() {
68-
return (
69-
<Navigator
70-
initialRoute = { {name: 'pushActivity' }}
71-
configureScene = { this.configureScene }
72-
renderScene = { this.renderScene } />
73-
);
21+
const PushDemoApp = StackNavigator({
22+
Home: {
23+
screen: PushActivity
24+
},
25+
Setting: {
26+
screen: SetActivity
27+
},
28+
Push: {
29+
screen: SecondActivity
7430
}
75-
}
31+
})
7632

7733
AppRegistry.registerComponent('PushDemoApp', () => PushDemoApp);

example/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"start": "node node_modules/react-native/local-cli/cli.js start"
88
},
99
"dependencies": {
10-
"jcore-react-native": "^1.1.5",
11-
"jpush-react-native": "^1.7.1",
10+
"jcore-react-native": "^1.1.7",
11+
"jpush-react-native": "^2.0.1",
1212
"react": "^16.0.0-alpha.12",
13-
"react-native": "^0.47.1",
13+
"react-native": "^0.44.2",
1414
"react-native-onesignal": "^3.0.4",
1515
"react-navigation": "^1.0.0-beta.11"
1616
},

example/react-native-android/push_activity.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ export default class PushActivity extends React.Component {
4646
}
4747

4848
jumpSetActivity() {
49-
this.props.navigator.push({
50-
name: 'setActivity'
51-
});
49+
this.props.navigation.navigate("Setting");
5250
}
5351

5452
jumpSecondActivity() {
5553
console.log("jump to SecondActivity");
56-
JPushModule.jumpToPushActivity("SecondActivity");
54+
JPushModule.jumpToPushActivityWithParams("SecondActivity", {
55+
hello: "world"
56+
});
5757
// this.props.navigator.push({
5858
// name: "second"
5959
// });
@@ -94,8 +94,7 @@ export default class PushActivity extends React.Component {
9494
});
9595
});
9696
JPushModule.notifyJSDidLoad((resultCode) => {
97-
if (resultCode === 0) {
98-
}
97+
if (resultCode === 0) {}
9998
});
10099
JPushModule.addReceiveCustomMsgListener((map) => {
101100
this.setState({
@@ -112,7 +111,8 @@ export default class PushActivity extends React.Component {
112111
JPushModule.addReceiveOpenNotificationListener((map) => {
113112
console.log("Opening notification!");
114113
console.log("map.extra: " + map.extras);
115-
JPushModule.jumpToPushActivity("SecondActivity");
114+
this.jumpSecondActivity();
115+
// JPushModule.jumpToPushActivity("SecondActivity");
116116
});
117117
JPushModule.addGetRegistrationIdListener((registrationId) => {
118118
console.log("Device register succeed, registrationId " + registrationId);

example/react-native-android/second.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export default class second extends React.Component {
3030
}
3131
}
3232

33+
componentDidMount() {
34+
JPushModule.addReceiveExtrasListener((map) => {
35+
console.log("Got extra, key: hello, value: " + map.hello);
36+
});
37+
}
38+
3339
onButtonPress = () => {
3440
console.log("will jump to setting page");
3541
let navigator = this.props.navigator;

example/react-native-android/set_activity.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import React from 'react';
44
import ReactNative from 'react-native';
55
const {
6-
BackAndroid,
76
Text,
87
View,
98
TextInput,
@@ -29,20 +28,9 @@ export default class SetActivity extends React.Component {
2928
this.setCustomStyle = this.setCustomStyle.bind(this);
3029
}
3130

32-
componentDidMount() {
33-
BackAndroid.addEventListener('hardwareBackPress', () => {
34-
const navigator = this.props.navigator;
35-
if (navigator.getCurrentRoutes().length > 1) {
36-
navigator.pop();
37-
return true;
38-
}
39-
return false;
40-
});
41-
}
31+
componentDidMount() {}
4232

43-
componentWillUnmount() {
44-
BackAndroid.removeEventListener('hardwareBackPress');
45-
}
33+
componentWillUnmount() {}
4634

4735
setTag() {
4836
if (this.state.tag !== undefined) {

index.d.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)