From 6cc55f7b197fd19701872f7f11c177f1d2e8a407 Mon Sep 17 00:00:00 2001 From: yogevbd Date: Mon, 10 Dec 2018 19:51:26 +0200 Subject: [PATCH] Add notification received in foreground listener --- .../java/com/wix/reactnativenotifications/Defs.java | 1 + .../core/notification/PushNotification.java | 8 ++++++++ docs/notificationsEvents.md | 3 +++ index.android.js | 12 ++++++++++++ 4 files changed, 24 insertions(+) diff --git a/android/src/main/java/com/wix/reactnativenotifications/Defs.java b/android/src/main/java/com/wix/reactnativenotifications/Defs.java index fff5174..26a8b7a 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/Defs.java +++ b/android/src/main/java/com/wix/reactnativenotifications/Defs.java @@ -7,5 +7,6 @@ public interface Defs { String TOKEN_RECEIVED_EVENT_NAME = "remoteNotificationsRegistered"; String NOTIFICATION_RECEIVED_EVENT_NAME = "notificationReceived"; + String NOTIFICATION_RECEIVED_FOREGROUND_EVENT_NAME = "notificationReceivedInForeground"; String NOTIFICATION_OPENED_EVENT_NAME = "notificationOpened"; } diff --git a/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java b/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java index fef71e3..3ea5eb3 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java +++ b/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java @@ -21,6 +21,7 @@ import com.wix.reactnativenotifications.core.ProxyService; import static com.wix.reactnativenotifications.Defs.NOTIFICATION_OPENED_EVENT_NAME; import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_EVENT_NAME; +import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_FOREGROUND_EVENT_NAME; public class PushNotification implements IPushNotification { @@ -61,6 +62,9 @@ public class PushNotification implements IPushNotification { public void onReceived() throws InvalidNotificationException { postNotification(null); notifyReceivedToJS(); + if (mAppLifecycleFacade.isAppVisible()) { + notifiyReceivedForegroundNotificationToJS(); + } } @Override @@ -186,6 +190,10 @@ public class PushNotification implements IPushNotification { mJsIOHelper.sendEventToJS(NOTIFICATION_RECEIVED_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext()); } + private void notifiyReceivedForegroundNotificationToJS() { + mJsIOHelper.sendEventToJS(NOTIFICATION_RECEIVED_FOREGROUND_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext()); + } + private void notifyOpenedToJS() { mJsIOHelper.sendEventToJS(NOTIFICATION_OPENED_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext()); } diff --git a/docs/notificationsEvents.md b/docs/notificationsEvents.md index 13e021b..f024ce4 100644 --- a/docs/notificationsEvents.md +++ b/docs/notificationsEvents.md @@ -72,6 +72,9 @@ import {NotificationsAndroid} from 'react-native-notifications'; NotificationsAndroid.setNotificationReceivedListener((notification) => { console.log("Notification received on device", notification.getData()); }); +NotificationsAndroid.setNotificationReceivedInForegroundListener((notification) => { + console.log("Notification received on device", notification.getData()); +}); NotificationsAndroid.setNotificationOpenedListener((notification) => { console.log("Notification opened by device user", notification.getData()); }); diff --git a/index.android.js b/index.android.js index 324d23a..974388d 100644 --- a/index.android.js +++ b/index.android.js @@ -4,6 +4,7 @@ import NotificationAndroid from "./notification"; const RNNotifications = NativeModules.WixRNNotifications; let notificationReceivedListener; +let notificationReceivedInForegroundListener; let notificationOpenedListener; let registrationTokenUpdateListener; @@ -23,6 +24,10 @@ export class NotificationsAndroid { notificationReceivedListener = DeviceEventEmitter.addListener("notificationReceived", (notification) => listener(new NotificationAndroid(notification))); } + static setNotificationReceivedInForegroundListener(listener) { + notificationReceivedInForegroundListener = DeviceEventEmitter.addListener("notificationReceivedInForeground", (notification) => listener(new NotificationAndroid(notification))); + } + static clearNotificationReceivedListener() { if (notificationReceivedListener) { notificationReceivedListener.remove(); @@ -30,6 +35,13 @@ export class NotificationsAndroid { } } + static clearNotificationReceivedInForegroundListener() { + if (notificationReceivedInForegroundListener) { + notificationReceivedInForegroundListener.remove(); + notificationReceivedInForegroundListener = null; + } + } + static setRegistrationTokenUpdateListener(listener) { registrationTokenUpdateListener = DeviceEventEmitter.addListener("remoteNotificationsRegistered", listener); } -- 2.26.2