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

Conversation

kylekurz
Copy link
Contributor

Summary

iOS throws a warning that registerForRemoteNotifications must be called
from the main thread only, this just wraps that call in a block to
ensure it always runs there, regardless of how the containing function
is called.

Test Plan

Ran on a device under debug and saw the thread safety warning go away

What's required for testing (prerequisites)?

Physical device

What are the steps to reproduce (after prerequisites)?

Call the method below from a thread other than the main UI thread:
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

Compatibility

OS Implemented
iOS ✅❌
Android ✅❌

Checklist

  • [X ] I have tested this on a device and a simulator
  • I added the documentation in README.md
  • I mentioned this change in CHANGELOG.md
  • I updated the typed files (TS and Flow)
  • I added a sample use of the API in the example project (example/App.js)

iOS throws a warning that registerForRemoteNotifications must be called
from the main thread only, this just wraps that call in a block to
ensure it always runs there, regardless of how the containing function
is called.
@kylekurz kylekurz changed the title Use main thread Use main thread for requesting notification permissions. Mar 19, 2020
@kylekurz
Copy link
Contributor Author

Ping on this...

Copy link

@Avarcal Avarcal left a comment

Choose a reason for hiding this comment

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

Great job!

@Avarcal
Copy link

Avarcal commented Mar 31, 2020

Ping this. I'm waiting for this fix.

@twboc
Copy link

twboc commented Mar 31, 2020

How come this is not merged to the code base??? I mean this is such a big improvement over what we had previously. In case of newer devices without the main thread resolution we won't be able to receive any notifications. Where is the library maintainer and what is he doing? This is already 2 weeks for a fix that is so essential and obvious to check that it makes my eyes bleed!

Copy link

@twboc twboc left a comment

Choose a reason for hiding this comment

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

Modern problems need modern solutions. Please merge this to the codebase. In newer devices we might have problems with receiving any notifications. This is a simple threading fix!!!

@dominiczaq
Copy link

In defence of the maintainer, this is an open source project. Nobody pays for work on this. If it's critical for your app you can always use push-notificaitons-ios from your fork and change to upstream when it's merged.

Copy link

@thehappydinoa thehappydinoa left a comment

Choose a reason for hiding this comment

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

Would love this to be merged (@Naturalclar)

[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.

@Naturalclar
Copy link
Collaborator

Sorry for the late response!
I'll test the change and have it published by end of today 👍

@tahourj
Copy link
Contributor

tahourj commented Apr 1, 2020 via email

Copy link
Collaborator

@Naturalclar Naturalclar left a comment

Choose a reason for hiding this comment

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

Thank you so much 👍

@Naturalclar Naturalclar changed the title Use main thread for requesting notification permissions. chore: Use main thread for requesting notification permissions. Apr 1, 2020
@Naturalclar Naturalclar merged commit bf19cc1 into react-native-push-notification:master Apr 1, 2020
@Naturalclar
Copy link
Collaborator

Published v1.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants