Skip to content

chore: Use main thread for requesting notification permissions. #90

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

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ios/RNCPushNotificationIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
if (error != NULL) {
reject(@"-1", @"Error - Push authorization request failed.", error);
} else {
[RCTSharedApplication() registerForRemoteNotifications];
dispatch_async(dispatch_get_main_queue(), ^(void){
[RCTSharedApplication() registerForRemoteNotifications];
});
[UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You should run the getNotificationSettingsWithCompletionHandler: call into Main Thread as well.

Copy link
Contributor Author

@kylekurz kylekurz Apr 1, 2020

Choose a reason for hiding this comment

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

My goal was to change the minimum amount to clear Apple's warnings. They do not show a warning about calling that function from another thread, so I didn't change it. This PR is simply to clean up Main Thread warnings, so this suggested addition is out of scope.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep but as you get it this code was running in background thread so the getNotificationSettingsWithCompletionHandler: will be too even if you didn't get warning by Apple is good pratice in iOS to register observer in main thread.
By experience we faced issues where when you don't register on main thread your observer completion was never fired.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I disagree. You shouldn't be doing more work on the Main (UI) thread than you have to. If Apple is happy to let you grab those settings from a non-UI thread, you should do so. The Main thread should be reserved for actions that require user interaction or drawing of elements.

resolve(RCTPromiseResolveValueForUNNotificationSettings(settings));
}];
Expand Down