diff --git a/android/src/main/java/com/wix/reactnativenotifications/Defs.java b/android/src/main/java/com/wix/reactnativenotifications/Defs.java index fff51742cb3fb8ad9c888479bdead9b70d063558..26a8b7a0383c642fa3e17102d582e502d05646eb 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 fef71e340af64adcc3f91cd6dbb3b480dcf77533..3ea5eb3d0527f734ffb89969939dacc210883794 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 13e021ba2a9cfb5c0f607fc595a51fb2e5daa985..1a1c0b975ba15bcfa0714c2d9b05c66e77b30a3b 100644 --- a/docs/notificationsEvents.md +++ b/docs/notificationsEvents.md @@ -70,7 +70,10 @@ import {NotificationsAndroid} from 'react-native-notifications'; // On Android, we allow for only one (global) listener per each event type. NotificationsAndroid.setNotificationReceivedListener((notification) => { - console.log("Notification received on device", notification.getData()); + console.log("Notification received on device in background or foreground", notification.getData()); +}); +NotificationsAndroid.setNotificationReceivedInForegroundListener((notification) => { + console.log("Notification received on device in foreground", 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 324d23adb87fbb9ae0db6187dc7135b73860a4aa..974388d164752ec10af4052eca95fd671d712b2a 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); }