Skip to content

Commit

Permalink
Merge pull request #310 from jpush/dev
Browse files Browse the repository at this point in the history
add new API
  • Loading branch information
KenChoi1992 authored Aug 7, 2017
2 parents 11c7103 + 904ff7c commit 7198491
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 133 deletions.
39 changes: 36 additions & 3 deletions android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
Expand Down Expand Up @@ -121,6 +122,16 @@ public void resumePush() {
Logger.toast(mContext, "Resume push success");
}

@ReactMethod
public void crashLogOFF() {
JPushInterface.stopCrashHandler(getReactApplicationContext());
}

@ReactMethod
public void crashLogON() {
JPushInterface.initCrashHandler(getReactApplicationContext());
}

@ReactMethod
public void notifyJSDidLoad(Callback callback) {
// send cached event
Expand Down Expand Up @@ -157,8 +168,10 @@ private static void sendEvent() {
.emit(mEvent, map);
break;
case CONNECTION_CHANGE:
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(mEvent, mCachedBundle.getBoolean(JPushInterface.EXTRA_CONNECTION_CHANGE, false));
if (mCachedBundle != null) {
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(mEvent, mCachedBundle.getBoolean(JPushInterface.EXTRA_CONNECTION_CHANGE, false));
}
break;
}
mEvent = null;
Expand Down Expand Up @@ -446,7 +459,7 @@ public void onReceive(Context context, Intent data) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else {
intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
intent.putExtras(mCachedBundle);
context.startActivity(intent);
Expand Down Expand Up @@ -575,6 +588,26 @@ public void jumpToPushActivity(String activityName) {

}

@ReactMethod
public void jumpToPushActivityWithParams(String activityName, ReadableMap map) {
Logger.d(TAG, "Jumping to " + activityName);
try {
Intent intent = new Intent();
if (null != map) {
while (map.keySetIterator().hasNextKey()) {
String key = map.keySetIterator().nextKey();
String value = map.getString(key);
intent.putExtra(key, value);
}
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(mRAC, mRAC.getPackageName() + "." + activityName);
mRAC.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}

@ReactMethod
public void finishActivity() {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.jpush.reactnativejpush;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
Expand All @@ -23,6 +24,11 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
});
}

// RN 0.47 remove this method
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
Expand Down
6 changes: 3 additions & 3 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ android {

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

// Run this once to be able to run the application with BUCK
Expand Down
17 changes: 17 additions & 0 deletions example/android/app/src/com/pushdemo/SecondActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package com.pushdemo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.facebook.react.ReactActivity;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class SecondActivity extends ReactActivity {

private final static String KEY = "hello";
private final static String RECEIVE_EXTRA_EVENT = "receiveExtras";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (null != intent) {
String value = intent.getStringExtra("key");
Log.i("SecondActivity", "Got intent, key: " + KEY + " value: " + value);
WritableMap map = Arguments.createMap();
map.putString(KEY, value);
getReactInstanceManager().getCurrentReactContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(RECEIVE_EXTRA_EVENT, map);
}
}

@Override
Expand Down
70 changes: 13 additions & 57 deletions example/index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,22 @@ import SecondActivity from './react-native-android/second';
const {
AppRegistry,
BackAndroid,
Navigator,

} = ReactNative;
import {
StackNavigator
} from 'react-navigation';
import JPushModule from 'jpush-react-native';

var navigator;
class PushDemoApp extends React.Component {

constructor(props) {
super(props);
}

componentDidMount() {
console.log("index did start!");
var navigator = this.navigator;
BackAndroid.addEventListener('hardwareBackPress', function() {
if (navigator && navigator.getCurrentRoutes().length > 1) {
navigator.pop();
return true;
}
return false;
});
}

componentWillUnmount() {
BackAndroid.removeEventListener('hardwareBackPress');
}

configureScene(route) {
return Navigator.SceneConfigs.FadeAndroid;
}

renderScene(router, navigator) {
var Component = null;
this.navigator = navigator;
switch (router.name) {
case "pushActivity":
Component = PushActivity;
break;
case "setActivity":
Component = SetActivity;
break;
case "second":
Component = SecondActivity;
break;
}

return <Component navigator = { navigator } />
}



render() {
return (
<Navigator
initialRoute = { {name: 'pushActivity' }}
configureScene = { this.configureScene }
renderScene = { this.renderScene } />
);
const PushDemoApp = StackNavigator({
Home: {
screen: PushActivity
},
Setting: {
screen: SetActivity
},
Push: {
screen: SecondActivity
}
}
})

AppRegistry.registerComponent('PushDemoApp', () => PushDemoApp);
6 changes: 3 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"start": "node node_modules/react-native/local-cli/cli.js start"
},
"dependencies": {
"jcore-react-native": "^1.1.5",
"jpush-react-native": "^1.7.1",
"jcore-react-native": "^1.1.7",
"jpush-react-native": "^2.0.1",
"react": "^16.0.0-alpha.12",
"react-native": "^0.47.1",
"react-native": "^0.44.2",
"react-native-onesignal": "^3.0.4",
"react-navigation": "^1.0.0-beta.11"
},
Expand Down
14 changes: 7 additions & 7 deletions example/react-native-android/push_activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ export default class PushActivity extends React.Component {
}

jumpSetActivity() {
this.props.navigator.push({
name: 'setActivity'
});
this.props.navigation.navigate("Setting");
}

jumpSecondActivity() {
console.log("jump to SecondActivity");
JPushModule.jumpToPushActivity("SecondActivity");
JPushModule.jumpToPushActivityWithParams("SecondActivity", {
hello: "world"
});
// this.props.navigator.push({
// name: "second"
// });
Expand Down Expand Up @@ -94,8 +94,7 @@ export default class PushActivity extends React.Component {
});
});
JPushModule.notifyJSDidLoad((resultCode) => {
if (resultCode === 0) {
}
if (resultCode === 0) {}
});
JPushModule.addReceiveCustomMsgListener((map) => {
this.setState({
Expand All @@ -112,7 +111,8 @@ export default class PushActivity extends React.Component {
JPushModule.addReceiveOpenNotificationListener((map) => {
console.log("Opening notification!");
console.log("map.extra: " + map.extras);
JPushModule.jumpToPushActivity("SecondActivity");
this.jumpSecondActivity();
// JPushModule.jumpToPushActivity("SecondActivity");
});
JPushModule.addGetRegistrationIdListener((registrationId) => {
console.log("Device register succeed, registrationId " + registrationId);
Expand Down
6 changes: 6 additions & 0 deletions example/react-native-android/second.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export default class second extends React.Component {
}
}

componentDidMount() {
JPushModule.addReceiveExtrasListener((map) => {
console.log("Got extra, key: hello, value: " + map.hello);
});
}

onButtonPress = () => {
console.log("will jump to setting page");
let navigator = this.props.navigator;
Expand Down
16 changes: 2 additions & 14 deletions example/react-native-android/set_activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import React from 'react';
import ReactNative from 'react-native';
const {
BackAndroid,
Text,
View,
TextInput,
Expand All @@ -29,20 +28,9 @@ export default class SetActivity extends React.Component {
this.setCustomStyle = this.setCustomStyle.bind(this);
}

componentDidMount() {
BackAndroid.addEventListener('hardwareBackPress', () => {
const navigator = this.props.navigator;
if (navigator.getCurrentRoutes().length > 1) {
navigator.pop();
return true;
}
return false;
});
}
componentDidMount() {}

componentWillUnmount() {
BackAndroid.removeEventListener('hardwareBackPress');
}
componentWillUnmount() {}

setTag() {
if (this.state.tag !== undefined) {
Expand Down
Empty file added index.d.ts
Empty file.
Loading

0 comments on commit 7198491

Please sign in to comment.