diff --git a/Libraries/ReactIOS/renderApplication.js b/Libraries/ReactIOS/renderApplication.js index 64e26126c..176dfa72a 100644 --- a/Libraries/ReactIOS/renderApplication.js +++ b/Libraries/ReactIOS/renderApplication.js @@ -15,12 +15,10 @@ function renderApplication(RootComponent, initialProps, rootTag) { rootTag, 'Expect to have a valid rootTag, instead got ', rootTag ); - var pushNotification = initialProps.launchOptions && - initialProps.launchOptions.remoteNotification && - new PushNotificationIOS(initialProps.launchOptions.remoteNotification); + var initialNotification = PushNotificationIOS.popInitialNotification(); React.render( , rootTag diff --git a/Libraries/Utilities/PushNotificationIOS.js b/Libraries/Utilities/PushNotificationIOS.js index 0cd8a6db6..390fa933e 100644 --- a/Libraries/Utilities/PushNotificationIOS.js +++ b/Libraries/Utilities/PushNotificationIOS.js @@ -5,8 +5,12 @@ */ 'use strict'; +var NativeModules = require('NativeModules'); var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter'); +var RCTPushNotificationManager = NativeModules.RCTPushNotificationManager; + +var _initialNotification = RCTPushNotificationManager.initialNotification; var _notifHandlers = {}; var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived'; @@ -30,6 +34,14 @@ class PushNotificationIOS { _notifHandlers[handler] = null; } + + static popInitialNotification() { + var initialNotification = _initialNotification && + new PushNotificationIOS(_initialNotification); + _initialNotification = null; + return initialNotification; + } + constructor(nativeNotif) { this._data = {}; diff --git a/ReactKit/Modules/RCTPushNotificationManager.h b/ReactKit/Modules/RCTPushNotificationManager.h new file mode 100644 index 000000000..e0ba53a62 --- /dev/null +++ b/ReactKit/Modules/RCTPushNotificationManager.h @@ -0,0 +1,12 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#import + +extern NSString *const RKRemoteNotificationReceived; +extern NSString *const RKOpenURLNotification; + +@interface RCTPushNotificationManager : NSObject + +- (instancetype)initWithInitialNotification:(NSDictionary *)initialNotification NS_DESIGNATED_INITIALIZER; + +@end diff --git a/ReactKit/Modules/RCTPushNotificationManager.m b/ReactKit/Modules/RCTPushNotificationManager.m new file mode 100644 index 000000000..b895f4d28 --- /dev/null +++ b/ReactKit/Modules/RCTPushNotificationManager.m @@ -0,0 +1,64 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#import "RCTPushNotificationManager.h" + +#import "RCTAssert.h" +#import "RCTBridge.h" +#import "RCTEventDispatcher.h" + +NSString *const RKRemoteNotificationReceived = @"RemoteNotificationReceived"; +NSString *const RKOpenURLNotification = @"RKOpenURLNotification"; + +@implementation RCTPushNotificationManager +{ + NSDictionary *_initialNotification; +} + +@synthesize bridge = _bridge; + +- (instancetype)init +{ + return [self initWithInitialNotification:nil]; +} + +- (instancetype)initWithInitialNotification:(NSDictionary *)initialNotification +{ + if ((self = [super init])) { + _initialNotification = [initialNotification copy]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleRemoteNotificationReceived:) + name:RKRemoteNotificationReceived + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleOpenURLNotification:) + name:RKOpenURLNotification + object:nil]; + } + return self; +} + +- (void)handleRemoteNotificationReceived:(NSNotification *)notification +{ + [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceived" + body:[notification userInfo]]; +} + +- (void)handleOpenURLNotification:(NSNotification *)notification +{ + [_bridge.eventDispatcher sendDeviceEventWithName:@"openURL" + body:[notification userInfo]]; +} + +- (NSDictionary *)constantsToExport +{ + return @{ + @"initialNotification": _initialNotification ?: [NSNull null] + }; +} + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +@end