React-native-push-notification: ios notification permission popup rejection event

Created on 26 Oct 2016  路  11Comments  路  Source: zo0r/react-native-push-notification

How do I know if the user rejected the notification permission that pops up automatically in ios?

Stale

Most helpful comment

@benthewhite How did you avoid the cannot call requestPermissions twice before the first has returned issue?

All 11 comments

We've been sorting out exactly this problem. PushNotification.requestPermissions() returns a promise so you can use this to see what the returns result is. It resolves to an object of enabled bits including alert so if that is 1 then the user picked allow, if it is 0 then the user effectively picked deny. For our code we use something like this...

PushNotification.requestPermissions().then(res => {
  if (!res.alert) {
    // They picked deny :(
  } else {
   // They picked allow :D
  }
});

@benthewhite How did you avoid the cannot call requestPermissions twice before the first has returned issue?

I just cached the promise whilst its running and return that or create a new request if not.

var requestPromise = null;

function requestPerms() {
  if (requestPromise) return requestPromise;

  requestPromise = PushNotification.requestPermissions().then(res => {
    requestPromise = null;
    return res;
  }, err => {
    requestPromise = null;
  });

  return requestPromise;
}


// Doing the actual request
requestPerms().then(res => {
  if (!res.alert) {
    // They picked deny :(
  } else {
   // They picked allow :D
  }
});


@benthewhite
Shame, doesn't work for me. I cannot call requestPermissions() at all without running into this error.
What does your configuration object look like?

I'm running into the same issue with that error.

You should set "requestPermissions" to FALSE in your config. It's set on TRUE by default. So if you do PushNotification.configure({ .. }); & PushNotification.requestPermissions(); it will try to fire it twice hence the error.

I set requestPermissions to false but I still get that error every time I refresh my app.

class PushConfig extends Component {
  componentDidMount() {
    PushNotification.configure({
      // (optional) Called when Token is generated (iOS and Android)
      onRegister: (token) => {
        this.props.registerDevice(this.props.username, token);
      },

      // (required) Called when a remote or local notification is opened or received
      onNotification: (notification) => {
        console.log('notification', notification);
      },

      requestPermissions: false
    });
  }

  render() {
    return null;
  }
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({ registerDevice }, dispatch);
}

export default connect(null, mapDispatchToProps)(PushConfig);

and I ask for a permissions when authenticated in my singin component like

componentWillUpdate(nextProps) {
    if (nextProps.authenticated) {
      PushNotification.requestPermissions();
      Actions.next();
    }
  }

and PushConfig is mounted in the next component after authentication.

Any thoughts?

@ckim16 Was this ever resolved?

@tywoodpav Unfortunately, No. I just dismiss the error and manually reload myself currently.

@benjackwhite How do you call .then on sometihng that returns null without getting an error like "Cannot read property 'then' of null"

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DaniShalash picture DaniShalash  路  3Comments

selimonline picture selimonline  路  3Comments

atteia picture atteia  路  4Comments

cidevant picture cidevant  路  3Comments

Benzer1406 picture Benzer1406  路  3Comments