PushNotificationIOS.requestPermissions won't resolve if no event listeners are attached

Summary:
Resolves #13012

RCTPushNotificationManager uses startObserving to register for RCTRegisterUserNotificationSettings. According to the docs, the startObserving method won't be called until somebody subscribes to NotificationManagerIOS.

This means there is a scenario when the developer can call requestPermissions without subscribing to notifications first, but since RCTPushNotificationManager relies on NSNotificationCenter subscribtion, the result will never be returned.

When requesting permissions  the promise will resolve:
`PushNotificationIOS.requestPermissions().then(console.log);` without the need for calling `PushNotificationIOS.addEventListener()` first.
Closes https://github.com/facebook/react-native/pull/13263

Differential Revision: D4851767

Pulled By: javache

fbshipit-source-id: 2be8621e072ae1086014594bc986ca5590b5eb61
This commit is contained in:
Peter Pistorius 2017-04-07 11:11:16 -07:00 коммит произвёл Facebook Github Bot
Родитель 5767b98f4d
Коммит d03f9b7442
1 изменённых файлов: 10 добавлений и 4 удалений

Просмотреть файл

@ -142,6 +142,15 @@ RCT_EXPORT_MODULE()
selector:@selector(handleRemoteNotificationReceived:)
name:RCTRemoteNotificationReceived
object:nil];
[self startObservingRegistrationNotifications];
}
- (void)startObservingRegistrationNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRegisterUserNotificationSettings:)
name:RCTRegisterUserNotificationSettings
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationsRegistered:)
name:RCTRemoteNotificationsRegistered
@ -150,10 +159,6 @@ RCT_EXPORT_MODULE()
selector:@selector(handleRemoteNotificationRegistrationError:)
name:RCTErrorRemoteNotificationRegistrationFailed
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRegisterUserNotificationSettings:)
name:RCTRegisterUserNotificationSettings
object:nil];
}
- (void)stopObserving
@ -319,6 +324,7 @@ RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions
return;
}
[self startObservingRegistrationNotifications];
_requestPermissionsResolveBlock = resolve;
UIUserNotificationType types = UIUserNotificationTypeNone;