From bd0f9fbacb32ad98c9e9d547b27130616164e3af Mon Sep 17 00:00:00 2001 From: Emily Janzer Date: Wed, 19 Apr 2017 11:25:50 -0700 Subject: [PATCH] Only start observing in requestPermissions() if not already observing Reviewed By: javache Differential Revision: D4903744 fbshipit-source-id: d2700f59bae521de6c76f8a3d11d6d48f1c0e7d9 --- .../PushNotificationIOS/RCTPushNotificationManager.m | 10 ++++------ React/Modules/RCTEventEmitter.h | 3 +++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m index 55107370e..ec7a487e1 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m @@ -142,11 +142,6 @@ RCT_EXPORT_MODULE() selector:@selector(handleRemoteNotificationReceived:) name:RCTRemoteNotificationReceived object:nil]; - [self startObservingRegistrationNotifications]; -} - -- (void)startObservingRegistrationNotifications -{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRegisterUserNotificationSettings:) name:RCTRegisterUserNotificationSettings @@ -281,6 +276,8 @@ RCT_EXPORT_MODULE() }; _requestPermissionsResolveBlock(notificationTypes); + // Clean up listener added in requestPermissions + [self removeListeners:1]; _requestPermissionsResolveBlock = nil; } @@ -324,7 +321,8 @@ RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions return; } - [self startObservingRegistrationNotifications]; + // Add a listener to make sure that startObserving has been called + [self addListener:@"remoteNotificationsRegistered"]; _requestPermissionsResolveBlock = resolve; UIUserNotificationType types = UIUserNotificationTypeNone; diff --git a/React/Modules/RCTEventEmitter.h b/React/Modules/RCTEventEmitter.h index 58d37b0ab..f98195827 100644 --- a/React/Modules/RCTEventEmitter.h +++ b/React/Modules/RCTEventEmitter.h @@ -38,4 +38,7 @@ - (void)startObserving; - (void)stopObserving; +- (void)addListener:(NSString *)eventName; +- (void)removeListeners:(NSInteger)count; + @end