如何导航到导航屏幕收到一个信号通知?

时间:2019-07-30 05:48:07

标签: reactjs react-native react-navigation onesignal

我想导航到打开时的通知屏幕,但App.js中我没有收到任何导航道具。我如何从app.js导航到通知屏幕。 ................................................... ................................................... ...................................................

App.js

import OneSignal from 'react-native-onesignal'; // Import package from node modules

export default class App extends Component {

constructor(properties) {
    super(properties);
    OneSignal.init("YOUR_ONESIGNAL_APPID");

    OneSignal.addEventListener('received', this.onReceived);
    OneSignal.addEventListener('opened', this.onOpened);
    OneSignal.addEventListener('ids', this.onIds);
    OneSignal.configure();  // triggers the ids event
  }

  componentWillUnmount() {
    OneSignal.removeEventListener('received', this.onReceived);
    OneSignal.removeEventListener('opened', this.onOpened);
    OneSignal.removeEventListener('ids', this.onIds);
  }

  onReceived(notification) {
    console.log("Notification received: ", notification);
  }

  onOpened(openResult) {

    console.log('openResult: ', openResult);

    this.props.navigation.navigate("Notification");   /// there is no navigation props here in app.js
  }

  onIds(device) {
    console.log('Device info: ', device);
  }
}```

Navigate.js

```const DrawerNavigator = createDrawerNavigator(
  {
    Home: HomeScreen,
    Settings: SettingsScreen,
    About: About,
    Notification: Notification
  },
);

export default createAppContainer(createSwitchNavigator(
  {
    AuthLoading: AuthLoadingScreen,
    App: DrawerNavigator,
    Auth: AuthStack,
  },
  {
    initialRouteName: 'AuthLoading',
  }
));

2 个答案:

答案 0 :(得分:0)

navigation道具仅自动提供给屏幕。您的<App />组件不是导航屏幕,因此this.props.navigation将始终是未定义的。

您应将事件侦听器移至屏幕。例如,它可能是AuthLoadingScreen的一部分,因为它将始终被加载一次。

答案 1 :(得分:0)

您可以尝试将OpenSignal侦听器移至初始化导航的位置,然后使用导航。现在,您肯定会得到this.props.navigation undefined error。在这种情况下,您可以在AuthLoadingScreen中使用这些侦听器。