From 4c846f0d9e4ea61f9bc1dd2ff4f5c2cfb8131d4f Mon Sep 17 00:00:00 2001 From: Amit Davidi Date: Wed, 16 Nov 2016 20:39:00 +0200 Subject: [PATCH] Extensibility step 4: reorganize folders, fix js issues --- .../core/InitialNotification.java | 2 ++ .../core/NotificationIntentAdapter.java | 2 ++ .../core/ProxyService.java | 3 +++ .../core/RNNotificationsModule.java | 9 +++++--- .../INotificationsApplication.java | 4 +++- .../{ => notification}/IPushNotification.java | 2 +- .../{ => notification}/PushNotification.java | 8 ++++++- .../PushNotificationProps.java | 2 +- .../INotificationsDrawerApplication.java | 2 +- .../IPushNotificationsDrawer.java | 2 +- .../PushNotificationsDrawer.java | 5 ++++- .../gcm/GcmMessageHandlerService.java | 4 ++-- example/android/settings.gradle | 5 ++++- example/index.android.js | 10 +++++---- example/package.json | 2 +- ...tifications.android.js => index.android.js | 22 +------------------ notification.android.js | 20 +++++++++++++++++ package.json | 1 - 18 files changed, 65 insertions(+), 40 deletions(-) rename example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/{ => notification}/INotificationsApplication.java (62%) rename example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/{ => notification}/IPushNotification.java (90%) rename example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/{ => notification}/PushNotification.java (92%) rename example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/{ => notification}/PushNotificationProps.java (94%) rename example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/{ => notificationdrawer}/INotificationsDrawerApplication.java (63%) rename example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/{ => notificationdrawer}/IPushNotificationsDrawer.java (75%) rename example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/{ => notificationdrawer}/PushNotificationsDrawer.java (87%) rename example/notifications.android.js => index.android.js (82%) create mode 100644 notification.android.js diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/InitialNotification.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/InitialNotification.java index 499d6cf..3d3e5fe 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/InitialNotification.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/InitialNotification.java @@ -2,6 +2,8 @@ package com.wix.reactnativenotifications.core; import android.support.annotation.Nullable; +import com.wix.reactnativenotifications.core.notification.PushNotificationProps; + public class InitialNotification { private static PushNotificationProps sNotification; diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/NotificationIntentAdapter.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/NotificationIntentAdapter.java index 6868387..de89b84 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/NotificationIntentAdapter.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/NotificationIntentAdapter.java @@ -5,6 +5,8 @@ import android.content.Context; import android.content.Intent; import android.os.Bundle; +import com.wix.reactnativenotifications.core.notification.PushNotificationProps; + public class NotificationIntentAdapter { private static final int PENDING_INTENT_CODE = 0; private static final String PUSH_NOTIFICATION_EXTRA_NAME = "pushNotification"; diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/ProxyService.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/ProxyService.java index 4523b63..bbc10c8 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/ProxyService.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/ProxyService.java @@ -5,6 +5,9 @@ import android.content.Intent; import android.os.Bundle; import android.util.Log; +import com.wix.reactnativenotifications.core.notification.IPushNotification; +import com.wix.reactnativenotifications.core.notification.PushNotification; + public class ProxyService extends IntentService { private static final String TAG = ProxyService.class.getSimpleName(); diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/RNNotificationsModule.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/RNNotificationsModule.java index 6e554cb..5db1c32 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/RNNotificationsModule.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/RNNotificationsModule.java @@ -12,6 +12,9 @@ import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; +import com.wix.reactnativenotifications.core.notification.PushNotificationProps; +import com.wix.reactnativenotifications.core.notificationdrawer.IPushNotificationsDrawer; +import com.wix.reactnativenotifications.core.notificationdrawer.PushNotificationsDrawer; import com.wix.reactnativenotifications.gcm.GcmInstanceIdRefreshHandlerService; import static com.wix.reactnativenotifications.Defs.LOGTAG; @@ -36,7 +39,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements Log.d(LOGTAG, "Native module init"); startGcmIntentService(GcmInstanceIdRefreshHandlerService.EXTRA_IS_APP_INIT); - IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); + final IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); notificationsDrawer.onAppInit(); } @@ -65,7 +68,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements @Override public void onAppVisible() { - IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); + final IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); notificationsDrawer.onAppVisible(); } @@ -75,7 +78,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { - IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); + final IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); notificationsDrawer.onNewActivity(activity); } diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/INotificationsApplication.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/INotificationsApplication.java similarity index 62% rename from example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/INotificationsApplication.java rename to example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/INotificationsApplication.java index 1500514..6b46988 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/INotificationsApplication.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/INotificationsApplication.java @@ -1,8 +1,10 @@ -package com.wix.reactnativenotifications.core; +package com.wix.reactnativenotifications.core.notification; import android.content.Context; import android.os.Bundle; +import com.wix.reactnativenotifications.core.AppLifecycleFacade; + public interface INotificationsApplication { IPushNotification getPushNotification(Context context, Bundle bundle, AppLifecycleFacade facade); } diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/IPushNotification.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/IPushNotification.java similarity index 90% rename from example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/IPushNotification.java rename to example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/IPushNotification.java index 5b1d8e9..a142f49 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/IPushNotification.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/IPushNotification.java @@ -1,4 +1,4 @@ -package com.wix.reactnativenotifications.core; +package com.wix.reactnativenotifications.core.notification; public interface IPushNotification { class InvalidNotificationException extends Exception { diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/PushNotification.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java similarity index 92% rename from example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/PushNotification.java rename to example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java index 176cb56..f3cae79 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/PushNotification.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java @@ -1,4 +1,4 @@ -package com.wix.reactnativenotifications.core; +package com.wix.reactnativenotifications.core.notification; import android.app.Notification; import android.app.NotificationManager; @@ -14,7 +14,13 @@ import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.WritableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; +import com.wix.reactnativenotifications.core.AppLaunchHelper; +import com.wix.reactnativenotifications.core.AppLifecycleFacade; import com.wix.reactnativenotifications.core.AppLifecycleFacade.AppVisibilityListener; +import com.wix.reactnativenotifications.core.InitialNotification; +import com.wix.reactnativenotifications.core.NotificationIntentAdapter; +import com.wix.reactnativenotifications.core.ProxyService; +import com.wix.reactnativenotifications.core.notificationdrawer.PushNotificationsDrawer; import static com.wix.reactnativenotifications.Defs.NOTIFICATION_OPENED_EVENT_NAME; import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_EVENT_NAME; diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/PushNotificationProps.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java similarity index 94% rename from example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/PushNotificationProps.java rename to example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java index 33936b0..d4512ce 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/PushNotificationProps.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java @@ -1,4 +1,4 @@ -package com.wix.reactnativenotifications.core; +package com.wix.reactnativenotifications.core.notification; import android.os.Bundle; diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/INotificationsDrawerApplication.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/INotificationsDrawerApplication.java similarity index 63% rename from example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/INotificationsDrawerApplication.java rename to example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/INotificationsDrawerApplication.java index 13cffc7..fe17c74 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/INotificationsDrawerApplication.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/INotificationsDrawerApplication.java @@ -1,4 +1,4 @@ -package com.wix.reactnativenotifications.core; +package com.wix.reactnativenotifications.core.notificationdrawer; public interface INotificationsDrawerApplication { IPushNotificationsDrawer getPushNotificationsDrawer(); diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/IPushNotificationsDrawer.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/IPushNotificationsDrawer.java similarity index 75% rename from example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/IPushNotificationsDrawer.java rename to example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/IPushNotificationsDrawer.java index 13a1b30..6711baa 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/IPushNotificationsDrawer.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/IPushNotificationsDrawer.java @@ -1,4 +1,4 @@ -package com.wix.reactnativenotifications.core; +package com.wix.reactnativenotifications.core.notificationdrawer; import android.app.Activity; diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/PushNotificationsDrawer.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java similarity index 87% rename from example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/PushNotificationsDrawer.java rename to example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java index 6aedd99..c0aa923 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/PushNotificationsDrawer.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java @@ -1,9 +1,12 @@ -package com.wix.reactnativenotifications.core; +package com.wix.reactnativenotifications.core.notificationdrawer; import android.app.Activity; import android.app.NotificationManager; import android.content.Context; +import com.wix.reactnativenotifications.core.AppLaunchHelper; +import com.wix.reactnativenotifications.core.InitialNotification; + public class PushNotificationsDrawer implements IPushNotificationsDrawer { protected final Context mContext; diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/gcm/GcmMessageHandlerService.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/gcm/GcmMessageHandlerService.java index 9c60ce7..5a75a9a 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/gcm/GcmMessageHandlerService.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/gcm/GcmMessageHandlerService.java @@ -4,8 +4,8 @@ import android.os.Bundle; import android.util.Log; import com.google.android.gms.gcm.GcmListenerService; -import com.wix.reactnativenotifications.core.IPushNotification; -import com.wix.reactnativenotifications.core.PushNotification; +import com.wix.reactnativenotifications.core.notification.IPushNotification; +import com.wix.reactnativenotifications.core.notification.PushNotification; import com.wix.reactnativenotifications.core.ReactAppLifecycleFacade; import static com.wix.reactnativenotifications.Defs.LOGTAG; diff --git a/example/android/settings.gradle b/example/android/settings.gradle index 35b948e..588dd48 100644 --- a/example/android/settings.gradle +++ b/example/android/settings.gradle @@ -1 +1,4 @@ -include ':reactnativenotification', ':myapplication' +include ':myapplication' + +include ':reactnativenotification' +//project(':reactnativenotification').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-notification/android') diff --git a/example/index.android.js b/example/index.android.js index e6e9d55..b0af4b4 100644 --- a/example/index.android.js +++ b/example/index.android.js @@ -5,11 +5,10 @@ import { AppRegistry, StyleSheet, Text, - View, - TouchableHighlight + View } from 'react-native'; -import {NotificationsAndroid, PendingNotifications} from './notifications'; +import {NotificationsAndroid, PendingNotifications} from 'react-native-notifications'; let mainScreen; @@ -31,6 +30,8 @@ function onNotificationReceived(notification) { } } +// It's highly recommended to keep listeners registration at global scope rather than at screen-scope seeing that +// component mount and unmount lifecycle tend to be asymmetric! NotificationsAndroid.setRegistrationTokenUpdateListener(onPushRegistered); NotificationsAndroid.setNotificationOpenedListener(onNotificationOpened); NotificationsAndroid.setNotificationReceivedListener(onNotificationReceived); @@ -64,11 +65,12 @@ class MainComponent extends Component { console.log('ReactScreen', 'ReactScreen'); mainScreen = this; + + setInterval(this.onTick.bind(this), 1000); } componentDidMount() { console.log('ReactScreen', 'componentDidMount'); - setInterval(this.onTick.bind(this), 1000); PendingNotifications.getInitialNotification() .then((notification) => {console.log("getInitialNotification:", notification); this.setState({initialNotification: notification.getData()});}) .catch((err) => console.error("getInitialNotifiation failed", err)); diff --git a/example/package.json b/example/package.json index 8712369..29be054 100644 --- a/example/package.json +++ b/example/package.json @@ -7,7 +7,7 @@ }, "dependencies": { "babel-preset-react-native-stage-0": "^1.0.1", - "react": "^15.3.1", + "react": "15.3.1", "react-native": "0.34.0", "react-native-notifications": "../" }, diff --git a/example/notifications.android.js b/index.android.js similarity index 82% rename from example/notifications.android.js rename to index.android.js index 1fc1026..e0517bc 100644 --- a/example/notifications.android.js +++ b/index.android.js @@ -1,5 +1,5 @@ -require('react'); import {NativeModules, DeviceEventEmitter} from 'react-native'; +import NotificationAndroid from './notification'; const RNNotifications = NativeModules.WixRNNotifications; @@ -7,26 +7,6 @@ let notificationReceivedListener; let notificationOpenedListener; let registrationTokenUpdateListener; -/** A wrapper to align Android with iOS in terms on notification structure. */ -class NotificationAndroid { - - constructor(notification) { - this.data = notification; - } - - getData() { - return this.data; - } - - getTitle() { - return this.data.title; - } - - getMessage() { - return this.data.body; - } -} - export class NotificationsAndroid { static setRegistrationTokenUpdateListener(listener) { registrationTokenUpdateListener = DeviceEventEmitter.addListener('remoteNotificationsRegistered', listener); diff --git a/notification.android.js b/notification.android.js new file mode 100644 index 0000000..bb2ace8 --- /dev/null +++ b/notification.android.js @@ -0,0 +1,20 @@ +/** A wrapper to align Android with iOS in terms on notification structure. */ +export default class NotificationAndroid { + + constructor(notification) { + this.data = notification; + } + + getData() { + return this.data; + } + + getTitle() { + return this.data.title; + } + + getMessage() { + return this.data.body; + } +} + diff --git a/package.json b/package.json index e7431aa..ec648ba 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,6 @@ "bugs": { "url": "https://github.com/wix/react-native-notifications/issues" }, - "main": "index.ios.js", "babel": { "presets": [ "react-native" -- 2.26.2