Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Conversation

peterp
Copy link
Contributor

@peterp peterp commented Apr 3, 2017

Motivation

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.

Test Plan

When requesting permissions the promise will resolve:
PushNotificationIOS.requestPermissions().then(console.log); without the need for calling PushNotificationIOS.addEventListener() first.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Apr 3, 2017
@peterp peterp force-pushed the issue-13012-push-notification-ios-request-permissions branch 2 times, most recently from dbb3cca to dff2a98 Compare April 3, 2017 10:00
@ericvicenti
Copy link
Contributor

cc @frantic, does this fix make sense?

@peterp peterp force-pushed the issue-13012-push-notification-ios-request-permissions branch from dff2a98 to b2398f8 Compare April 3, 2017 15:16
@@ -249,6 +269,7 @@ - (void)handleRemoteNotificationReceived:(NSNotification *)notification
- (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
{
[self sendEventWithName:@"remoteNotificationsRegistered" body:notification.userInfo];
[self stopObservingRegisteredNotifications];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to stopObservingRegisteredNotifications? IIRC when registering for push (not local) notifications, RCTRegisterUserNotificationSettings is called first, followed by RCTRemoteNotificationsRegistered if we got push token.

I don't see a reason to stop observing because it adds complexity without much gain.

@frantic
Copy link
Contributor

frantic commented Apr 3, 2017

It's clean and awesome, thanks @peterp! My only suggestion is to not manually unsubscribe from notification center, because it might introduce some racing bugs.

@peterp peterp force-pushed the issue-13012-push-notification-ios-request-permissions branch from b2398f8 to 07a6afb Compare April 3, 2017 19:39
@peterp peterp force-pushed the issue-13012-push-notification-ios-request-permissions branch from 07a6afb to 9796f57 Compare April 3, 2017 19:41
@peterp peterp force-pushed the issue-13012-push-notification-ios-request-permissions branch from 9796f57 to fc2c7eb Compare April 4, 2017 06:31
@onpaws
Copy link

onpaws commented Apr 6, 2017

Nice to see this issue get some attention! Cheers gents.

@frantic
Copy link
Contributor

frantic commented Apr 6, 2017

@facebook-github-bot shipit

@facebook-github-bot facebook-github-bot added the Import Started This pull request has been imported. This does not imply the PR has been approved. label Apr 7, 2017
@facebook-github-bot
Copy link
Contributor

@javache has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

thotegowda pushed a commit to thotegowda/react-native that referenced this pull request May 7, 2017
…eners are attached

Summary:
Resolves facebook#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 facebook#13263

Differential Revision: D4851767

Pulled By: javache

fbshipit-source-id: 2be8621e072ae1086014594bc986ca5590b5eb61
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Import Started This pull request has been imported. This does not imply the PR has been approved.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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