javascript - onesignal 与 react 导航和 redux : i am not able to navigate to screen when notification is opened

标签 javascript react-native react-redux react-navigation onesignal

问题:
我需要在打开 onesignal 通知时导航到某个屏幕。

情况:
我无法从我的应用程序组件进行导航,因为导航器尚未定义。
我无法从 RootContainer 组件进行导航,因为导航器尚未定义。

我考虑了调度操作而不是导航,但相同:
我无法从我的应用程序组件分派(dispatch)操作,因为商店尚未定义。
我无法从 RootContainer 组件分派(dispatch)操作,因为导航尚未定义。

这是 App.js:

... import

const persistConfig = {
    key: 'root',
    version: 1,
    storage,
    blacklist: ['navigation']
};

const persistedReducer = persistReducer(persistConfig, reducer);

const store = createStore(persistedReducer, {
    is_logged_in: false,
    login_type: null,
    access_token: null
},
    applyMiddleware(logger));

const persistor = persistStore(store);
class App extends Component {

    constructor(props) {
        super(props);

        OneSignal.init("...");

        this.onOpened = this.onOpened.bind(this);

        OneSignal.addEventListener('received', this.onReceived);
        OneSignal.addEventListener('opened', this.onOpened);
        OneSignal.addEventListener('ids', this.onIds);

        this.state = {
            notification_offer_clicked: null
        };
    }

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

    onOpened(openResult) {
        this.setState({
            notification_offer_clicked: openResult.notification.payload.additionalData.offer
        });
    }

    render () {
        return (
            <Provider store={store}>
                <PersistGate loading={null} persistor={persistor}>
                    <RootContainer notification_offer_clicked={this.state.notification_offer_clicked} />
                </PersistGate>
            </Provider>
        )
    }
}

export default DebugConfig.useReactotron
  ? console.tron.overlay(App)
  : App

如您所见,我尝试将 notification_offer_clicked 属性传递给 RootContainer 来传播事件。
该 Prop 已正确传递,但我不知道如何从那里继续。

这是 Container.js:

... import

class RootContainer extends Component {
    shouldComponentUpdate(nextProps, nextState) {
        //if (nextProps.notification_offer_clicked) {
        //    this.props.navigation.navigate('Offer', { 'offer' : nextProps.notification_offer_clicked });
        //}
        return true;
    }

    render() {
        return (
            <AppNavigator />
        );
    }
}

const mapStateToProps = (state) => {
    return {
        is_logged_in: state.is_logged_in,
    };
};

// wraps dispatch to create nicer functions to call within our component
const mapDispatchToProps = (dispatch) => ({
    startup: () => dispatch(StartupActions.startup())
});

export default connect(mapStateToProps, mapDispatchToProps)(RootContainer)

AppNavigator 是一个 StackNavigator 变量,所以我不知道如何(如果可能的话)将 props 传递给它。

问题是如果我尝试减少注释

this.props.navigation.navigate...

line 我收到错误原因 navigation 尚未在 RootContainer 中定义。

我的问题是:当我打开 onesignal 通知时,如何导航到屏幕(或简单地调度 redux 操作)?

最佳答案

为什么不在主屏幕中初始化事件监听器? 假设您有这样的导航架构,

SCREEN1: 
- SUBSCREEN1,
- SUBSCREEN2,

现在,您可以将事件监听器添加到 Screen1 组件中。 为了采取动态方法,您可以从一个信号发送路线名称, 例如。一个信号数据包是..

    notifications: {
      payload: {
        additionalData: {
        routeName: 'SUBSCREEN1',
        params: {
          id: 12
        }
       }
     }
   }

然后,您可以有效地从父屏幕导航到嵌套屏幕,因为您也获得了路线名称和 Prop ... 然后,您可以在 onOpened 监听器上定义一个简单的函数,例如 onOpened...

onOpened = (data) => {
// some validation if there is a data or not,,, if there is
this.props.navigation.navigate(data.notification.payload.additionalData.routeName,data.notification.payload.addtionalData.params);
}

在这里,您传递了一个routeName和 Prop ,以便它可以导航到具有所需 Prop 的任何屏幕,

注意:您始终需要在导航之前验证数据,以减少错误和崩溃的数量。而且您的根组件上不需要事件监听器。

您不需要它,因为当您单击通知时,它会等待直到收到事件监听器,并且一旦收到事件监听器,它就会执行最后一个通知,我知道这是一个奇怪的行为 一个信号

关于javascript - onesignal 与 react 导航和 redux : i am not able to navigate to screen when notification is opened,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53903400/

相关文章:

javascript - JavaScript 中的 hr 标签

reactjs - 即使我们只需要渲染一个组件,所有组件都会被渲染,在react-redux中

react-native - 上传文件但设置 Content-Type

javascript - react /Redux : where do I create + put the 'master' application state?

javascript - 如何使用 Normalizr 对其进行标准化?

javascript - 有没有办法在 Chrome 控制台中将 JavaScript 函数视为对象?

javascript - 在 div 转换时修复 child 的位置

javascript - 带有 Rails 3.1 的 ActiveAdmin 破坏了 javascript

javascript - 错误 : Unable to resolve module `@react-native-community/toolbar-android`

javascript - 手动调用 React render() 函数?