diff --git a/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainApplication.java b/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainApplication.java index fc8df0061a891a76553af18232eb096c75287283..350b838f89fca340854c8d557d884e745a30eff2 100644 --- a/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainApplication.java +++ b/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainApplication.java @@ -23,7 +23,7 @@ public class MainApplication extends Application implements ReactApplication { protected List getPackages() { return Arrays.asList( new MainReactPackage(), - new RNNotificationsPackage() + new RNNotificationsPackage(MainApplication.this) ); } }; diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java index 215308a587277d09dad7e6e78d1c032830da4e2d..e8495c45278d29aa9170fad0355fe8eb7011a525 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java @@ -1,5 +1,7 @@ package com.wix.reactnativenotifications; +import android.app.Application; + import com.facebook.react.ReactPackage; import com.facebook.react.bridge.JavaScriptModule; import com.facebook.react.bridge.NativeModule; @@ -13,9 +15,16 @@ import java.util.List; public class RNNotificationsPackage implements ReactPackage { + + final Application mApplication; + + public RNNotificationsPackage(Application application) { + mApplication = application; + } + @Override public List createNativeModules(ReactApplicationContext reactContext) { - return Arrays.asList(new RNNotificationsModule(reactContext)); + return Arrays.asList(new RNNotificationsModule(mApplication, reactContext)); } @Override diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/AppLaunchHelper.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/AppLaunchHelper.java index af7cfb8af3e59484a1b0f8c25eb6e84224f91b9e..b098e7c5d26b7fa1f679da3a10e3b93dbbb888dc 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/AppLaunchHelper.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/AppLaunchHelper.java @@ -34,7 +34,9 @@ public class AppLaunchHelper { public static boolean isLaunchIntentsActivity(Activity activity) { final Intent helperIntent = activity.getPackageManager().getLaunchIntentForPackage(activity.getPackageName()); - return activity.getLocalClassName().equals(helperIntent.getComponent().getClassName()); + final String activityName = activity.getComponentName().getClassName(); + final String launchIntentActivityName = helperIntent.getComponent().getClassName(); + return activityName.equals(launchIntentActivityName); } public static boolean isLaunchIntent(Intent intent) { 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/INotificationsDrawerApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..13cffc708a197fbd1491413483ac1a438e6aa65a --- /dev/null +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/INotificationsDrawerApplication.java @@ -0,0 +1,5 @@ +package com.wix.reactnativenotifications.core; + +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/IPushNotificationsDrawer.java new file mode 100644 index 0000000000000000000000000000000000000000..13a1b3039f496661ca5454afb27710aa1aebb3f9 --- /dev/null +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/IPushNotificationsDrawer.java @@ -0,0 +1,11 @@ +package com.wix.reactnativenotifications.core; + +import android.app.Activity; + +public interface IPushNotificationsDrawer { + void onAppInit(); + void onAppVisible(); + void onNewActivity(Activity activity); + + void onNotificationOpened(); +} 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 4b02ad23fb74f0bf613af6a0abd94d9b2ee3e226..499d6cf2cba2f41f6fa5a814154a5b0a3d811f23 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 @@ -9,6 +9,10 @@ public class InitialNotification { sNotification = pushNotificationProps; } + public static void clear() { + sNotification = null; + } + @Nullable public static PushNotificationProps get() { return sNotification; 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/PushNotification.java index b630c73c0bdeb633f21b936ee8639ce56212459f..176cb560ad8b3cc824dca77df8952f0cc18d43f1 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/PushNotification.java @@ -58,7 +58,7 @@ public class PushNotification implements IPushNotification { @Override public void onOpened() { digestNotification(); - deleteAllPostedNotifications(); + PushNotificationsDrawer.get(mContext).onNotificationOpened(); } @Override @@ -170,9 +170,4 @@ public class PushNotification implements IPushNotification { final Intent intent = AppLaunchHelper.getLaunchIntent(mContext); mContext.startActivity(intent); } - - protected void deleteAllPostedNotifications() { - final NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.cancelAll(); - } } 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/PushNotificationsDrawer.java new file mode 100644 index 0000000000000000000000000000000000000000..6aedd99b9ffe2c34116cd60e595ffd64d9e30ef6 --- /dev/null +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/PushNotificationsDrawer.java @@ -0,0 +1,51 @@ +package com.wix.reactnativenotifications.core; + +import android.app.Activity; +import android.app.NotificationManager; +import android.content.Context; + +public class PushNotificationsDrawer implements IPushNotificationsDrawer { + + protected final Context mContext; + + public PushNotificationsDrawer(Context context) { + mContext = context; + } + + public static IPushNotificationsDrawer get(Context context) { + final Context appContext = context.getApplicationContext(); + if (appContext instanceof INotificationsDrawerApplication) { + return ((INotificationsDrawerApplication) appContext).getPushNotificationsDrawer(); + } + + return new PushNotificationsDrawer(context); + } + + @Override + public void onAppInit() { + clearAll(); + } + + @Override + public void onAppVisible() { + clearAll(); + } + + @Override + public void onNewActivity(Activity activity) { + if (AppLaunchHelper.isLaunchIntentsActivity(activity) && + !AppLaunchHelper.isLaunchIntent(activity.getIntent())) { + InitialNotification.clear(); + } + } + + @Override + public void onNotificationOpened() { + clearAll(); + } + + protected void clearAll() { + final NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.cancelAll(); + } +} 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 c6fa5ea8f0a3cc5e4c21629f3ccc6c5bf18b543a..6e554cbc2b29de3dfff69692976a41ec8d1ed9d8 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 @@ -1,7 +1,10 @@ package com.wix.reactnativenotifications.core; +import android.app.Activity; +import android.app.Application; import android.content.Context; import android.content.Intent; +import android.os.Bundle; import android.util.Log; import com.facebook.react.bridge.Arguments; @@ -13,12 +16,14 @@ import com.wix.reactnativenotifications.gcm.GcmInstanceIdRefreshHandlerService; import static com.wix.reactnativenotifications.Defs.LOGTAG; -public class RNNotificationsModule extends ReactContextBaseJavaModule { +public class RNNotificationsModule extends ReactContextBaseJavaModule implements AppLifecycleFacade.AppVisibilityListener, Application.ActivityLifecycleCallbacks { - public RNNotificationsModule(ReactApplicationContext reactContext) { + public RNNotificationsModule(Application application, ReactApplicationContext reactContext) { super(reactContext); - ReactAppLifecycleFacade.get().onAppInit(reactContext); + ReactAppLifecycleFacade.get().init(reactContext); + ReactAppLifecycleFacade.get().addVisibilityListener(this); + application.registerActivityLifecycleCallbacks(this); } @Override @@ -29,14 +34,17 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule { @Override public void initialize() { Log.d(LOGTAG, "Native module init"); + startGcmIntentService(GcmInstanceIdRefreshHandlerService.EXTRA_IS_APP_INIT); - final Context appContext = getReactApplicationContext().getApplicationContext(); - final Intent tokenFetchIntent = new Intent(appContext, GcmInstanceIdRefreshHandlerService.class); - tokenFetchIntent.putExtra(GcmInstanceIdRefreshHandlerService.EXTRA_IS_APP_INIT, true); - appContext.startService(tokenFetchIntent); + IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); + notificationsDrawer.onAppInit(); } - + @ReactMethod + public void refreshToken() { + Log.d(LOGTAG, "Native method invocation: refreshToken()"); + startGcmIntentService(GcmInstanceIdRefreshHandlerService.EXTRA_MANUAL_REFRESH); + } @ReactMethod public void getInitialNotification(final Promise promise) { @@ -55,13 +63,50 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule { } } - @ReactMethod - public void refreshToken() { - Log.d(LOGTAG, "Native method invocation: refreshToken()"); + @Override + public void onAppVisible() { + IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); + notificationsDrawer.onAppVisible(); + } + + @Override + public void onAppNotVisible() { + } + + @Override + public void onActivityCreated(Activity activity, Bundle savedInstanceState) { + IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); + notificationsDrawer.onNewActivity(activity); + } + + @Override + public void onActivityStarted(Activity activity) { + } + + @Override + public void onActivityResumed(Activity activity) { + } + + @Override + public void onActivityPaused(Activity activity) { + } + + @Override + public void onActivityStopped(Activity activity) { + } + + @Override + public void onActivitySaveInstanceState(Activity activity, Bundle outState) { + } + + @Override + public void onActivityDestroyed(Activity activity) { + } + protected void startGcmIntentService(String extraFlag) { final Context appContext = getReactApplicationContext().getApplicationContext(); final Intent tokenFetchIntent = new Intent(appContext, GcmInstanceIdRefreshHandlerService.class); - tokenFetchIntent.putExtra(GcmInstanceIdRefreshHandlerService.EXTRA_MANUAL_REFRESH, true); + tokenFetchIntent.putExtra(extraFlag, true); appContext.startService(tokenFetchIntent); } } diff --git a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/ReactAppLifecycleFacade.java b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/ReactAppLifecycleFacade.java index f1d663969e8843bf9a2655cda5969bb2f63af783..26ef28f52d1a5b96e53b8fedeb45e79b2709c08e 100644 --- a/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/ReactAppLifecycleFacade.java +++ b/example/android/reactnativenotification/src/main/java/com/wix/reactnativenotifications/core/ReactAppLifecycleFacade.java @@ -12,7 +12,7 @@ import static com.wix.reactnativenotifications.Defs.LOGTAG; public class ReactAppLifecycleFacade implements AppLifecycleFacade { - private static ReactAppLifecycleFacade sInstance = new ReactAppLifecycleFacade(); + private static final ReactAppLifecycleFacade sInstance = new ReactAppLifecycleFacade(); private ReactContext mReactContext; private boolean mIsVisible; @@ -22,10 +22,8 @@ public class ReactAppLifecycleFacade implements AppLifecycleFacade { return sInstance; } - public synchronized void onAppInit(ReactContext reactContext) { + public void init(ReactContext reactContext) { mReactContext = reactContext; - mIsVisible = false; - reactContext.addLifecycleEventListener(new LifecycleEventListener() { @Override public void onHostResume() { @@ -35,15 +33,14 @@ public class ReactAppLifecycleFacade implements AppLifecycleFacade { @Override public void onHostPause() { + Log.d(LOGTAG, "onHostPause"); switchToInvisible(); } @Override public void onHostDestroy() { - switchToInvisible(); Log.d(LOGTAG, "onHostDestroy"); - mReactContext.removeLifecycleEventListener(this); - mReactContext = null; + switchToInvisible(); } }); } diff --git a/example/index.android.js b/example/index.android.js index c63a5d3386cf45de1ac8fe3ef28d480a90ea4af9..e6e9d5529b13f3f9a1f3b16117cc4bbdb861fa60 100644 --- a/example/index.android.js +++ b/example/index.android.js @@ -11,6 +11,30 @@ import { import {NotificationsAndroid, PendingNotifications} from './notifications'; +let mainScreen; + +function onPushRegistered() { + if (mainScreen) { + mainScreen.onPushRegistered(); + } +} + +function onNotificationOpened(notification) { + if (mainScreen) { + mainScreen.onNotificationOpened(notification) + } +} + +function onNotificationReceived(notification) { + if (mainScreen) { + mainScreen.onNotificationReceived(notification) + } +} + +NotificationsAndroid.setRegistrationTokenUpdateListener(onPushRegistered); +NotificationsAndroid.setNotificationOpenedListener(onNotificationOpened); +NotificationsAndroid.setNotificationReceivedListener(onNotificationReceived); + const styles = StyleSheet.create({ container: { flex: 1, @@ -36,22 +60,24 @@ class MainComponent extends Component { this.state = { elapsed: 0, lastNotification: undefined - } - } + }; - componentWillMount() { - NotificationsAndroid.setRegistrationTokenUpdateListener(this.onPushRegistered.bind(this)); - NotificationsAndroid.setNotificationOpenedListener(this.onNotificationOpened.bind(this)); - NotificationsAndroid.setNotificationReceivedListener(this.onNotificationReceived.bind(this)); + console.log('ReactScreen', 'ReactScreen'); + mainScreen = this; } 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)); } + componentWillUnmount() { + console.log('ReactScreen', 'componentWillUnmount'); + } + onTick() { this.setState({elapsed: this.state.elapsed + 1}); }