mirror of
https://github.com/zhigang1992/react-native-notifications.git
synced 2026-06-13 01:28:11 +08:00
Merge pull request #271 from wix/foregroundListener
Foreground listener
This commit is contained in:
@@ -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";
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user