question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
franticcommented, Mar 21, 2017

@binoy14 sure!

0reactions
cpojercommented, Feb 13, 2019

This issue has been moved to react-native-community/react-native-push-notification-ios#3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react native - PushNotificationIOS.requestPermissions() callback
There is the option to add an event listener for when a device registers for push notifications. PushNotificationIOS.
Read more >
Handling Events - React
Handling events with React elements is very similar to handling events on DOM ... Instead, just provide a listener when the element is...
Read more >
React Native push notification using Batch and Fastlane
Request push notifications permission (the user will be shown a native dialog asking for permission); If permission is granted then APNS (Apple ...
Read more >
@react-native-community/push-notification-ios - npm
Event type. handler, function, Yes, Listener. Valid events are: notification : Fired when a remote notification is received.
Read more >
React Native Push Notification API for iOS - Morioh
Attaches a listener to remote or local notification events while the app is ... If you do not call this method your background...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found