首页 > 解决方案 > 无法从推送通知导航到所选屏幕

问题描述

当用户单击或按下通知时,我希望打开一个特定的屏幕。我经历了各种 github 问题和堆栈溢出问题和答案,但是似乎没有一个答案或选择的方式来解决这个问题。

最初我使用 PushController 类来处理 onNotification 函数,但是我没有将 configure 和 onNotification 函数移动到组件中,并安装在创建本地通知的组件中。

this.props.navigation.navigate('RouteName'); 

不工作。它说this.props.navigationthis.props.navigation.navigate未定义。

PushNotification.
            onNotification(notification) {
                console.log('onNotification')
                console.log( notification );
this.props.navigation.navigate('RegisterRoute');
            },

        });

从通知路由或导航到所选屏幕的正确方法是什么。我在 react-native-push-notification 问题和文档中找不到直接答案。

编辑:此代码可以在我调用/发出本地通知的类中找到。通知被触发和调用,当单击通知或 onNotification 时,我想导航或路由到另一个屏幕。

组件内部确实安装了。

PushNotification.configure({

            onNotification(notification) {
                console.log('onNotification')
                console.log( notification );
this.props.navigation.navigate('ChosenScreen');
            },


        });

调用并创建本地通知的函数

function sendNotification(title, pushMessage){
        PushNotification.localNotification({
        message: pushMessage, 
        title: title,
        userInteraction: true,
        alertAction: 'default',// (optional) default: view

       autoCancel: true, 
       largeIcon: "logo", 
       smallIcon: "logo",
       vibrate: true, 
       vibration: 300,

       alertAction: 'default',
       userInfo: 'default'

      });

}

标签: react-nativereact-native-navigationreact-native-push-notification

解决方案


this.props.navigation was undefined意味着您的组件没有导航道具。您必须使用withNavigation这是一个更高阶的组件。


推荐阅读