我使用react-navigation
和react-native-push-notification
。如何在onNotification
回调中打开某个StackNavigator's
屏幕?应在以下情况下工作:
- 应用程序关闭
- 应用程序位于前台
- 应用程序在后台
我现在只需要它在Android上工作。
我尝试在组件中传递一个回调函数给notification:
_handleClick() {
PushNotification.localNotification({
foreground: false
userInteraction: false
message: 'My Notification Message'
onOpen: () => { this.props.navigation.navigate("OtherScreen") },
})
}
在PushNotification
配置中触发onOpen
:
onNotification: function(notification) {
notification.onOpen()
}
但是,函数似乎不能传递给通知,除非一个值是一个字符串,它被忽略,导致onOpen
未定义。
4条答案
按热度按时间b4lqfgs41#
好吧,看来我得发布自己的解决方案:)
rqenqsqc2#
我在Firebase的官方网站上找到了这个解决方案,这似乎是最好的例子/样本工作。下面是示例片段和链接。希望它能帮助到其他人。
网址:https://rnfirebase.io/messaging/notifications#handling-interaction
nnsrf1az3#
鉴于我是如何使用传统的
react-native-firebase
,我想我会发布我对这个问题的解决方案,因为它与上面使用RN-firebase V6的答案之一略有不同。我的解决方案只是略有不同,此解决方案适用于react-native-firebase
v5.x的通知处理:of1yzvn44#
这些信息可能对某人有用。
在我的场景中,如果用户收到通知但尚未登录,则应用程序不应将其重定向到所需的屏幕。
如果用户不在登录屏幕上,用户按下通知,我们应该重定向他们(在我的情况下是屏幕2)。
为此,我将导航参考发送到我的
NotificationHelper
使用:
@react-navigation/native: 6.X
@react-native-firebase/messaging: 17.x
@notifee/react-native: 7.x
验证码:
MainScreen.tsx
NotificationHelper.tsx