diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index 732d4c0294c9af..688c085477bcb1 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -19,7 +19,8 @@ var _notifHandlers = {}; var _initialNotification = RCTPushNotificationManager && RCTPushNotificationManager.initialNotification; -var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived'; +var DEVICE_NOTIF_EVENT_FOREGROUND = 'remoteNotificationReceivedForeground'; +var DEVICE_NOTIF_EVENT_BACKGROUND = 'remoteNotificationReceivedBackground'; var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered'; /** @@ -55,19 +56,30 @@ class PushNotificationIOS { * * Valid events are: * - * - `notification` : Fired when a remote notification is received. The - * handler will be invoked with an instance of `PushNotificationIOS`. + * - `notificationForeground` : Fired when a remote notification is received + * and the app is running in foreground. The handler will be invoked with an + * instance of `PushNotificationIOS`. + * - `notificationBackground` : Same as `notificationForeground`, only it will + * only be fired when app is opening from background. * - `register`: Fired when the user registers for remote notifications. The * handler will be invoked with a hex string representing the deviceToken. */ static addEventListener(type: string, handler: Function) { invariant( - type === 'notification' || type === 'register', - 'PushNotificationIOS only supports `notification` and `register` events' + type === 'notificationForeground' || type === 'notificationBackground' || type === 'register', + 'PushNotificationIOS only supports `notificationForeground`, `notificationBackground`, and `register` events' ); - if (type === 'notification') { + + if (type === 'notificationForeground') { _notifHandlers[handler] = RCTDeviceEventEmitter.addListener( - DEVICE_NOTIF_EVENT, + DEVICE_NOTIF_EVENT_FOREGROUND, + (notifData) => { + handler(new PushNotificationIOS(notifData)); + } + ); + } else if (type === 'notificationBackground') { + _notifHandlers[handler] = RCTDeviceEventEmitter.addListener( + DEVICE_NOTIF_EVENT_BACKGROUND, (notifData) => { handler(new PushNotificationIOS(notifData)); } @@ -80,6 +92,7 @@ class PushNotificationIOS { } ); } + } /** @@ -140,8 +153,8 @@ class PushNotificationIOS { */ static removeEventListener(type: string, handler: Function) { invariant( - type === 'notification' || type === 'register', - 'PushNotificationIOS only supports `notification` and `register` events' + type === 'notificationForeground' || type === 'notificationBackground' || type === 'register', + 'PushNotificationIOS only supports `notificationForeground`, `notificationBackground`, and `register` events' ); if (!_notifHandlers[handler]) { return; diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m index 12d6ba45643b19..5f5b1af2da49a6 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m @@ -22,7 +22,8 @@ #endif -NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived"; +NSString *const RCTRemoteNotificationReceivedForeground = @"RemoteNotificationReceivedForeground"; +NSString *const RCTRemoteNotificationReceivedBackground = @"RemoteNotificationReceivedBackground"; NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered"; @implementation RCTPushNotificationManager @@ -38,9 +39,15 @@ - (instancetype)init { if ((self = [super init])) { [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(handleRemoteNotificationReceived:) - name:RCTRemoteNotificationReceived + selector:@selector(handleRemoteNotificationReceivedForeground:) + name:RCTRemoteNotificationReceivedForeground object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleRemoteNotificationReceivedBackground:) + name:RCTRemoteNotificationReceivedBackground + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRemoteNotificationsRegistered:) name:RCTRemoteNotificationsRegistered @@ -84,14 +91,30 @@ + (void)application:(UIApplication *)application didRegisterForRemoteNotificatio + (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification { - [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived - object:self - userInfo:notification]; + UIApplicationState state = [application applicationState]; + + if (state == UIApplicationStateActive) { + //app is in foreground + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceivedForeground + object:self + userInfo:notification]; + } else { + //app is in background + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceivedBackground + object:self + userInfo:notification]; + } +} + +- (void)handleRemoteNotificationReceivedForeground:(NSNotification *)notification +{ + [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceivedForeground" + body:[notification userInfo]]; } -- (void)handleRemoteNotificationReceived:(NSNotification *)notification +- (void)handleRemoteNotificationReceivedBackground:(NSNotification *)notification { - [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceived" + [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceivedBackground" body:[notification userInfo]]; }