没有导航道具的React导航在发布模式下不工作(Android签名应用程序)

没有导航道具的React导航在发布模式下不工作(Android签名应用程序),android,firebase,react-native,react-navigation,Android,Firebase,React Native,React Navigation,我正在实现一个接收firebase推送通知的react本机应用程序。当通知到达时,应用程序导航到屏幕以显示通知。 我采用了以下方法: “在没有导航道具的情况下导航” 当我用调试模式测试它时,它工作得非常好。但当我在发布模式(android签名应用)下测试时,它不起作用 特别是,当应用程序打开时通知到达时,它将无法工作。没有错误消息,应用程序冻结,大约30秒后,应用程序崩溃 以下是包裹信息: "react": "16.8.3", "react-i18next": "10.12.2

我正在实现一个接收firebase推送通知的react本机应用程序。当通知到达时,应用程序导航到屏幕以显示通知。 我采用了以下方法: “在没有导航道具的情况下导航”

当我用调试模式测试它时,它工作得非常好。但当我在发布模式(android签名应用)下测试时,它不起作用

特别是,当应用程序打开时通知到达时,它将无法工作。没有错误消息,应用程序冻结,大约30秒后,应用程序崩溃

以下是包裹信息:

    "react": "16.8.3",
    "react-i18next": "10.12.2",
    "react-native": "0.59.10",
    "react-native-firebase": "5.5.6",
    "react-native-gesture-handler": "1.3.0",
    "react-navigation": "3.11.1",
基本上,我试过这个 “在没有导航道具的情况下导航”

同样,这一条也是:

我使用的不是类组件而是函数组件

//Navigator.js

const switchNavigator = createSwitchNavigator({
  ResolveAuth: ResolveAuthScreen,
  loginFlow: createStackNavigator({
    Signin: SigninScreen,
    Signup: SignupScreen
  }),
  helpFlow: createStackNavigator({
    Help: HelpScreen,
  }, {headerLayoutPreset: 'center'}),
  mainFlow: createBottomTabNavigator({
    Ask: createStackNavigator({
      AskMain: AskScreen,
      AskWait: AskWaitScreen,
    }, {headerLayoutPreset: 'center'}),
    Chat: createStackNavigator({
      ChatList: ChatListScreen,
      Chatting: ChatScreen,
    }, {headerLayoutPreset: 'center'}),
    Profile: createStackNavigator({
      Account: AccountScreen,
      AccountEdit: AccountEditScreen,
      ProfileContract: ProfileScreen
    }
    , {headerLayoutPreset: 'center'})
  },
...
export default createAppContainer(switchNavigator);
//App.js

import Navigator from './Navigator';
import { useTranslation } from 'react-i18next';
import { navigate, setNavigator } from './src/navigationRef';

const App = Navigator;

export default () => {

  // setup language
  const { t } = useTranslation();

  // use effect
  useEffect(() => {

    // notification listener (triggered when a particular notification has been received)
    // if the app is foreground, we need to navigate the screen
    const listenerFG = firebase.notifications().onNotification((notification: Notification) => {
      console.log('onNotification', notification);
      Alert.alert(
        t('AppScreen.title'),
        t('AppScreen.message'),
        [
          {text: t('yes'), onPress: () => navigate('Help', { notificationBody: notification })},
        ],
        {cancelable: true},
      );
    });

   listenerForAppClosed();

   return () => {
      listenerFG();
    }
  }, []);

  return (
    <App ref={(navigator) => { setNavigator(navigator) }} /> 
  );
在调试模式下,使用“导航”(“任何屏幕”)就像一个符咒,但在发布模式下,它不起作用

但一件奇怪的事情是,下面的导航是有效的。当应用程序不是前台状态时,用户打开推送通知

//App.js的一部分

// listen the notification being opened or clicked when the app is closed
  const listenerForAppClosed = async() => {
    // app closed
    const notificationOpen: NotificationOpen = await firebase.notifications().getInitialNotification();
    if (notificationOpen) {
      // app was opened by a notification
      console.log('getInitialNotification', notificationOpen);
      // get information about the notification that was opened
      const notification: Notification = notificationOpen.notification;
      //// ignore the same notification id since the same notification is received again, don't know why.
      // get noti id from storage
      const notiId = await AsyncStorage.getItem('notiId');
      // set noti id to storage
      await AsyncStorage.setItem('notiId', notification.notificationId);     
      if (notification.notificationId === notiId) {
        console.log('notification id is the same');       
      } else { 
        console.log('navigating to helpscreen...');   
        // navigate to Help screen
        navigate('Help', { notificationBody: notification });
      }
    }
  }
这个问题发生在Android emulator和设备(Android9)上

为什么导航(“帮助”)在发布模式下不工作?我搜索了很多文档,我觉得它也应该在发布模式下工作


有没有其他方法可以从顶层(如App.js)导航到屏幕?

我找到了问题的根源。 我测试了一些东西。 我想知道在发布模式下非常简单的应用程序是否能够正确导航。 所以,我只是关注了这个帖子:

以下是我所做的: -创建了两个屏幕:主页和通知。 -使用最新版本重新创建应用程序-native@0.60.6反应-navigation@4.0.9 -发送的云消息不是来自应用程序,而是来自firebase云消息

成功了!当通知到达时,应用程序导航到通知屏幕

所以我试图找出问题的根源。 -尝试添加更多屏幕 -添加了更多的提供者和上下文 -已从应用程序发送消息

最后,我找到了来源。这就是我使用“navigateRef.js”的方式 最初我是这样使用的:

// App.js
import { navigate, setNavigator } from './src/navigationRef';
<App ref={(navigator) => { setNavigator(navigator) }} /> 
我只是使用了react navigation中的代码:

然后我工作了!我不知道这两种代码的区别。 第一个在调试模式下工作得很好,但在发布模式下没有,尤其是应用程序在前台


有人能告诉我区别吗?为什么第一个代码不起作用?

我找到了问题的根源。 我测试了一些东西。 我想知道在发布模式下非常简单的应用程序是否能够正确导航。 所以,我只是关注了这个帖子:

以下是我所做的: -创建了两个屏幕:主页和通知。 -使用最新版本重新创建应用程序-native@0.60.6反应-navigation@4.0.9 -发送的云消息不是来自应用程序,而是来自firebase云消息

成功了!当通知到达时,应用程序导航到通知屏幕

所以我试图找出问题的根源。 -尝试添加更多屏幕 -添加了更多的提供者和上下文 -已从应用程序发送消息

最后,我找到了来源。这就是我使用“navigateRef.js”的方式 最初我是这样使用的:

// App.js
import { navigate, setNavigator } from './src/navigationRef';
<App ref={(navigator) => { setNavigator(navigator) }} /> 
我只是使用了react navigation中的代码:

然后我工作了!我不知道这两种代码的区别。 第一个在调试模式下工作得很好,但在发布模式下没有,尤其是应用程序在前台

有人能告诉我区别吗?为什么第一个代码不起作用

// navigationRef.js
import { NavigationActions } from 'react-navigation';

let navigator;

// nav is coming from react navigation
export const setNavigator = navRef => {
  console.log('navigation ref', navRef);
  // set navigator
  navigator = navRef;
};

export const navigate = (routeName, params) => {
  console.log('[navigate dispatch] navigator', navigator);
  navigator.dispatch(
    NavigationActions.navigate({
      routeName,
      params
    })
  );
};
// App.js
import NavigationService from './src/NavigationService';
<App
    ref={navigationRef => 
      {NavigationService.setTopLevelNavigator(navigationRef);}}
/>
// NavigationService.js
import { NavigationActions } from 'react-navigation';

let _navigator;

function setTopLevelNavigator(navigatorRef) {
  _navigator = navigatorRef;
}

function navigate(routeName, params) {
  _navigator.dispatch(
    NavigationActions.navigate({
      routeName,
      params,
    })
  );
}

// add other navigation functions that you need and export them

export default {
  navigate,
  setTopLevelNavigator,
}