react-native: PushNotificationIOS.requestPermissions promise never resolves

I’m confused as to how this is supposed to be used. The RN PushNotificationIOS docs say:

This method returns a promise that will resolve when the user accepts, rejects, or if the permissions were previously rejected. The promise resolves to the current state of the permission.

But when I use the following, console.log() is never hit.

PushNotificationIOS.requestPermissions().then((permissions) => console.log(permissions));

What am I missing?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 9
  • Comments: 23 (10 by maintainers)

Most upvoted comments

Figured out something very strange…

In order for PushNotificationIOS.requestPermissions() to resolve the Promise, you need to add an event listener, like this:

PushNotificationIOS.addEventListener('register', (token) => {});

What’s even more interesting is that it doesn’t even have to be the 'register' event, it could be 'notification' or 'localNotification' and it will still cause the promise to resolve.

So this is what I ended up using anywhere I use requestPermissions() and needed to handle the promise:

componentWillMount() {
  // NOTE: You absolutely need this for requestPermissions() promise to resolve.   
  PushNotificationIOS.addEventListener('register', (token) => {});
}

componentWillUnmount() {
  PushNotificationIOS.removeEventListener('register', (token) => {});
}

Can at least a couple of you test this and confirm the behaviour. Once it’s reproducible on more than just my machine, I’ll see what’s the cause and issue a PR to fix it.

Thanks.

@joshuapinter ah, gotcha. I was able to repro; denying perms and running app again never resolves promise. As a stopgap the fix also works on my end, e.g.

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

Still getting this problem on 0.50.0

@joshuapinter’s solution works perfectly for me. Thanks!

@superandrew213 Are you sure, because they seem to be related, but different issues. That issue seems to be regarding calling requestPermissions() a second time before the first call has been resolved.