diff --git a/android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java b/android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java index 981fbf3a..e1e65d11 100644 --- a/android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java +++ b/android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java @@ -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; @@ -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 @@ -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; @@ -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); @@ -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 { diff --git a/android/src/main/java/cn/jpush/reactnativejpush/JPushPackage.java b/android/src/main/java/cn/jpush/reactnativejpush/JPushPackage.java index 9668579f..cf1e1a29 100644 --- a/android/src/main/java/cn/jpush/reactnativejpush/JPushPackage.java +++ b/android/src/main/java/cn/jpush/reactnativejpush/JPushPackage.java @@ -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; @@ -23,6 +24,11 @@ public List createNativeModules(ReactApplicationContext reactConte }); } + // RN 0.47 remove this method + public List> createJSModules() { + return Collections.emptyList(); + } + @Override public List createViewManagers(ReactApplicationContext reactContext) { return Collections.emptyList(); diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 7652e136..ccfe7bd3 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -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 diff --git a/example/android/app/src/com/pushdemo/SecondActivity.java b/example/android/app/src/com/pushdemo/SecondActivity.java index fceddb2b..6ccbf7c1 100644 --- a/example/android/app/src/com/pushdemo/SecondActivity.java +++ b/example/android/app/src/com/pushdemo/SecondActivity.java @@ -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 diff --git a/example/index.android.js b/example/index.android.js index 7e509bea..ac70f5d5 100644 --- a/example/index.android.js +++ b/example/index.android.js @@ -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 - } - - - - render() { - return ( - - ); +const PushDemoApp = StackNavigator({ + Home: { + screen: PushActivity + }, + Setting: { + screen: SetActivity + }, + Push: { + screen: SecondActivity } -} +}) AppRegistry.registerComponent('PushDemoApp', () => PushDemoApp); \ No newline at end of file diff --git a/example/package.json b/example/package.json index 23c47356..66def651 100644 --- a/example/package.json +++ b/example/package.json @@ -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" }, diff --git a/example/react-native-android/push_activity.js b/example/react-native-android/push_activity.js index 5132a269..289c31fe 100644 --- a/example/react-native-android/push_activity.js +++ b/example/react-native-android/push_activity.js @@ -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" // }); @@ -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({ @@ -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); diff --git a/example/react-native-android/second.js b/example/react-native-android/second.js index 9751b8b6..c2d55624 100644 --- a/example/react-native-android/second.js +++ b/example/react-native-android/second.js @@ -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; diff --git a/example/react-native-android/set_activity.js b/example/react-native-android/set_activity.js index 5c1a5a94..bd3c1bb3 100644 --- a/example/react-native-android/set_activity.js +++ b/example/react-native-android/set_activity.js @@ -3,7 +3,6 @@ import React from 'react'; import ReactNative from 'react-native'; const { - BackAndroid, Text, View, TextInput, @@ -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) { diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/index.js b/index.js index ffc3e9dd..69bc9d28 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,8 @@ const connectionChangeEvent = "connectionChange"; const getRegistrationIdEvent = "getRegistrationId"; // Android Only const openNotificationLaunchAppEvent = "openNotificationLaunchApp"; // iOS Only -const networkDidLogin = "networkDidLogin" // iOS Only +const networkDidLogin = "networkDidLogin"; // iOS Only +const receiveExtrasEvent = "receiveExtras"; // Android Only /** * Logs message to console with the [JPush] prefix @@ -66,6 +67,20 @@ export default class JPush { JPushModule.resumePush(); } + /** + * Android Only + */ + static crashLogOFF() { + JPushModule.crashLogOFF(); + } + + /** + * Android Only + */ + static crashLogON() { + JPushModule.crashLogON(); + } + /** * Android Only * @@ -261,6 +276,13 @@ export default class JPush { JPushModule.jumpToPushActivity(activityName); } + /** + * Android Only + */ + static jumpToPushActivityWithParams(activityName, map) { + JPushModule.jumpToPushActivityWithParams(activityName, map); + } + /** * Android Only */ @@ -302,7 +324,7 @@ export default class JPush { cb(registrationId); }); } - + /** * iOS Only * 取消监听:应用没有启动的状态点击推送打开应用 @@ -328,7 +350,7 @@ export default class JPush { cb(registrationId); }); } - + /** * iOS Only * @@ -439,7 +461,27 @@ export default class JPush { listeners[cb] = null; } - + /** + * 监听:收到 Native 下发的 extra 事件 + * @param {Function} cb = (map) => { } + * 返回 Object,属性和值在 Native 定义 + */ + static addReceiveExtrasListener(cb) { + listeners[cb] = DeviceEventEmitter.addListener(receiveExtrasEvent, + (map) => { + cb(map); + }); + } + + static removeReceiveExtrasListener(cb) { + if (!listeners[cb]) { + return; + } + listeners[cb].remove(); + listeners[cb] = null; + } + + /** * 获取 RegistrationId * @param {Function} cb = (String) => { } diff --git a/package.json b/package.json index 9f5d7fd4..194ea8e2 100644 --- a/package.json +++ b/package.json @@ -1,44 +1,49 @@ { - "name": "jpush-react-native", - "version": "2.0.1", - "description": "a jpush plugin for react native application", - "main": "index.js", - "scripts": { - "postinstall": "node postInstall.js" - }, - "repository": { - "type": "git", - "url": "git@github.com:jpush/jpush-react-native.git" - }, - "keywords": [ - "react", - "native", - "jpush" - ], - "author": "KenChoi, huminiOS", - "license": "ISC", - "bugs": { - "url": "https://github.com/jpush/jpush-react-native/issues" - }, - "homepage": "https://github.com/jpush/jpush-react-native#readme", - "peerDependencies": { - "jcore-react-native": ">= 1.1.5" - }, - "rnpm": { - "ios": { - "sharedLibraries": [ - "libz", - "UserNotifications", - "CoreTelephony", - "Security", - "CFNetwork", - "CoreFoundation", - "SystemConfiguration", - "Foundation", - "UIKit", - "libresolv", - "CoreGraphics" - ] - } - } + "name": "jpush-react-native", + "version": "2.0.2", + "description": "a jpush plugin for react native application", + "main": "index.js", + "scripts": { + "postinstall": "node postInstall.js" + }, + "types": "index.d.ts", + "repository": { + "type": "git", + "url": "git@github.com:jpush/jpush-react-native.git" + }, + "keywords": [ + "react", + "native", + "jpush" + ], + "author": "KenChoi, huminiOS", + "license": "ISC", + "bugs": { + "url": "https://github.com/jpush/jpush-react-native/issues" + }, + "homepage": "https://github.com/jpush/jpush-react-native#readme", + "peerDependencies": { + "jcore-react-native": ">= 1.1.5" + }, + "rnpm": { + "ios": { + "sharedLibraries": [ + "libz", + "UserNotifications", + "CoreTelephony", + "Security", + "CFNetwork", + "CoreFoundation", + "SystemConfiguration", + "Foundation", + "UIKit", + "libresolv", + "CoreGraphics" + ] + } + }, + "devDependencies": { + "babel-cli": "^6.24.1", + "babel-preset-env": "^1.6.0" + } } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..3a76082b --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "sourceMap": true + }, + "exclude": [ + "node_modules" + ] +} \ No newline at end of file