react-native - 无法从推送通知导航到所选屏幕
问题描述
当用户单击或按下通知时,我希望打开一个特定的屏幕。我经历了各种 github 问题和堆栈溢出问题和答案,但是似乎没有一个答案或选择的方式来解决这个问题。
最初我使用 PushController 类来处理 onNotification 函数,但是我没有将 configure 和 onNotification 函数移动到组件中,并安装在创建本地通知的组件中。
this.props.navigation.navigate('RouteName');
不工作。它说this.props.navigation
或this.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'
});
}
解决方案
this.props.navigation was undefined
意味着您的组件没有导航道具。您必须使用withNavigation这是一个更高阶的组件。
推荐阅读
- mysql - 对来自 3 个 SQL 表的数据进行分组
- php - WordPress 将 jQuery 移动到页脚,但依赖于它的脚本在它之前加载
- java - java中列表上的同步块
- c - 如何在 C 中识别 OCaml 可扩展变体类型值?
- vulkan - Vulkan 中先前子通道的输入附件
- python - UnicodeDecodeError:“ascii”编解码器无法解码位置 0 的字节 0xa7:不在序数范围内 (128)
- sockets - 在 Flutter 中使用 HttpServer 在 ServerSocket.bind 上不受支持的操作
- java - 如何为本地对象方法调用抛出异常?
- amazon-s3 - 如何允许 NuxtJS(app) 网页在 iFrame 中打开并存储在通过 CloudFront 提供的 S3 上?
- python - Python:使用正则表达式删除多次出现的标点符号?