【发布时间】:2020-08-22 16:05:39
【问题描述】:
- 当移动应用程序关闭时收到通知时,如何导航到移动应用程序中的某些视图/屏幕?
- 平台 反应原生 v0.61.5 通知包 =>
react-native-firebase v6.2.0 react-native-push-notification v3.1.9
反应导航 v4.0.10
【问题讨论】:
标签: react-native react-navigation react-native-firebase
react-native-firebase v6.2.0 react-native-push-notification v3.1.9
反应导航 v4.0.10
【问题讨论】:
标签: react-native react-navigation react-native-firebase
您可以使用 react-navigation 中的 NavigationActions。
App.js
<AppContainer
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);}}/>
导航服务.js
import { NavigationActions, StackActions } from 'react-navigation';
let _navigator;
function setTopLevelNavigator(navigatorRef) {
_navigator = navigatorRef;
}
function navigate(routeName, params) {
_navigator.dispatch(
NavigationActions.navigate({
routeName,
params,
})
);
}
export default {
navigate,
setTopLevelNavigator
}
现在在 firebase 的 onNotificationOpened 函数中,您必须写一行。
NavigationService.navigate('your screen', { param:paramValue })
【讨论】:
//您可以使用这种格式发送带有通知的数据
{ "to" : "FCM_token", “collapse_key”:“type_a”, “通知” : { “身体”:“测试”, "title": "测试标题"
}, “数据” : { “名称”:“xyz” } }
//现在你可以在监听器中获取数据了
const notificationOpen = await firebase.notifications().getInitialNotification();
如果(通知打开){
let {name} = notificationOpen.notification._data;
console.log("Notification data - ", name);
setInitialRoute(name);
}
//现在要声明状态变量并更改导航器的initialRoute....
const [initialRoute, setInitialRoute] = useState('ABC');
setInitialRoute(名称); // 在通知监听器中
//现在在导航容器中使用
<Stack.Navigator initialRouteName={initialRoute}>
</Stack.Navigator>
【讨论】: