Skip to content

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

Open
@cpojer

Description

@cpojer

This issue was originally created by @frantic as facebook/react-native#13012.


Description

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.

This should be fairly straightforward to solve for somebody new to React Native. Feel free to send a PR!

Reproduction

Have this in your JS file

PushNotificationIOS.requestPermissions().then(console.log);

You'll see iOS permissions dialog, however pressing "Allow" won't resolve the promise and nothing will be printed to the console.

Solution

Change the code to this, then uninstall and run the app again (needed to reset iOS permissions cache):

PushNotificationIOS.addEventListener('localNotification', () => {});
PushNotificationIOS.requestPermissions().then(console.log);

Now "Allow" button will work as expected, the promise will be resolved and you'll see log entry.

Additional Information

  • React Native version: 0.42.0
  • Platform: iOS
  • Operating System: macOS

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions