【问题标题】:navigating user on notification click app in background/foreground在后台/前台的通知点击应用程序上导航用户
【发布时间】:2018-02-19 16:01:08
【问题描述】:

我正在使用 REACT-NATIVE-FCM。

我的应用在 App.js 的堆栈导航器内有抽屉导航器。

在通知托盘中单击通知时,我希望用户直接转到某个屏幕。

在主页注册 FCM 事件仅在应用关闭时有效,而在前台应用中不会调用事件。它只会将您带到最后一个打开的屏幕。

我必须在每个屏幕上注册 FCM 事件,这对我来说看起来不太好。所以我有在 App.js 中注册 FCM 事件的解决方案,因为 App.js 在初始化应用程序时从 index.js 调用,但我无法在 App.js 中使用 this.props.navigation.navigate。

使用反应导航我们如何在声明堆栈后重定向用户。

APP.JS

    const Drawer = DrawerNavigator(
      {
        Dashboard: { screen: Dashboard },
        Screen1: { screen: Screen1 },
         Screen2: { screen: Screen2},
         Screen3: { screen: Screen3},
      },
      {
        navigationOptions: {
          gesturesEnabled: false,
        },
       initialRouteName: "Dashboard",
        contentOptions: {
          activeTintColor: "#e91e63"
        },
        drawerPosition: 'right',
        contentComponent: props => <SideBar {...props} />
      }
    );


    const AppNavigator = StackNavigator(
        {
            Home: { screen: Home },
           Drawer: { screen: Drawer },
            ScreenABC: { screen: ScreenABC },
            ScreenXYZ: { screen: ScreenXYZ }
        },
        {
             headerMode: "none",
        }
    );


    export default () =>
        <Root>
            <AppNavigator />
        </Root>;


    //CALLED WHEN APP IN FOREGROUND
    this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
    //alert('FCM.on: ' + JSON.stringify(notif));
if(notif.routeValue == 1)
{
this.props.navigation.navigate("Screen1")}
    });

    //CALLED WHEN APP IN BACKGROUND
    FCM.getInitialNotification().then(notif => {

       });

【问题讨论】:

    标签: react-native react-navigation react-native-fcm


    【解决方案1】:

    如果您无论如何都想真正进入某个屏幕。在调用 navigate 之前,您至少应该重置您的导航器状态。

    或者您也可以创建一个自定义操作,分派此操作并返回正确的导航器状态。

    这是我将导航器状态重置为主页的方法:

    export const homeNav = (state, action) => {
      let index;
    
      let routes;
    
      switch (action.type) {
    
     case types.RESET_HOME_NAV:
      routes = [...state.routes];
    
      for (let index = state.index; index > 0; --index) {
        routes.pop();
      }
    
      return {
        ...state,
        routes,
        index: 0,
      };
    
    default:
      return NavigatorHome.router.getStateForAction(action, state);
    }};
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-04
      • 2016-10-22
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      相关资源
      最近更新 更多