0

My index.ios.js responds to a push notifications. I want this to send the user to a different page if the phone is open. I am unsure how to do that. I am using OneSignals for push.

import React, { Component } from 'react';
import { ... } from 'react-native';

export default class Example extends Component {
    componentWillMount() {
      OneSignal.addEventListener('received', this.onReceived);
    }

    onReceived(notification) {
        // triggers on push
    }

    renderScene(route, navigator){ 
      return <route.component navigator={navigator} />
    }

    render() {
        return (
            <Navigator initialRoute={{component: Login}} 
            renderScene={this.renderScene.bind(this)} />
        )
    }   
}

AppRegistry.registerComponent('Example', () => Example);

What I tried

I tried to changed the state then re-render the whole app which works temporarily but locks you in that page because the state cannot update.

forceRerender(route, navigator){
  return <BarberShow navigator={navigator}  />
}

render() {
  if(this.state.notification){
    return <Navigator initialRoute={{component: AppointmentShow}} renderScene={this.forceRerender.bind(this)}/>
  }else{
    return <Navigator initialRoute={{component: Login}} renderScene={this.renderScene.bind(this)} />
  }
}
Alain Goldman
  • 2,896
  • 5
  • 43
  • 75

3 Answers3

1

Where did you changed the state in your code?

I'd also recommend you to use 'React-native-router-flux' which is a great module for managing navigation.

In case you use that :

import React, { Component } from 'react';
import { ... } from 'react-native';
import Actions from 'react-native-router-flux';

export default class Example extends Component {
    componentWillMount() {
      OneSignal.addEventListener('received', this.onReceived);
    }

    onReceived(notification) {
        // triggers on push
        Actions.customView({notification})
    }

    renderScene(route, navigator){ 
      return <route.component navigator={navigator} />
    }

    render() {
        return (
            <Navigator initialRoute={{component: Login}} 
            renderScene={this.renderScene.bind(this)} />
        )
    }   
}

AppRegistry.registerComponent('Example', () => Example);

You can easily navigate user to any view/component and send the notification data to that view as well.

Ata Mohammadi
  • 3,430
  • 6
  • 41
  • 69
1

For react native I recommend to use Ex-Navigation you can use it in both (react native and exponent) and it's really good and east.

1

Solution

All you have to do is add a global scope variable to keep track of navigator so that index.ios.js has access to it.

var _navigator;
export default class Example extends Component {
    componentWillMount() {
      OneSignal.addEventListener('received', this.onReceived);
    }

    onReceived(notification) {
        _navigator.push({
          component: NewPageHere
        });
    }

    renderScene(route, navigator){ 
      _navigator = navigator;
      return <route.component navigator={navigator} />
    }

    render() {
        return (
            <Navigator initialRoute={{component: Login}} 
            renderScene={this.renderScene.bind(this)} />
        )
    }   
}
Alain Goldman
  • 2,896
  • 5
  • 43
  • 75