android - 当应用程序处于后台状态 React native 时深度链接不起作用

标签 android react-native deep-linking

我正在创建基于 react native 的电子商务应用程序。这里我需要从 url shared 打开单个产品页面。实际上,当应用程序处于终止状态时它会工作,但如果应用程序处于后台/非 Activity 状态,它将不会工作。在后台/非 Activity 状态下打开时共享 url 变为空。我附上了我的代码。

// following code working for app killing state

componentWillMount() {

    if (Platform.OS === 'android') {
      console.log("Testing");debugger

      //Constants.OneTimeFlag == false;
          Linking.getInitialURL().then(url => {
            console.log(url);
            var str = url
            var name = str.split('/')[4]
            Constants.isLinking = true;
           this.setState({ shop_Id: name})


           if (str)
           {
            this.setState({ isFromHomeLinking:'FROM_LINK' })
            this.props.navigation.navigate('SingleProductScreen', { ListViewClickItemHolder: [this.state.shop_Id,1,this.state.isFromHomeLinking] });

           }


          });

    }

    else {
        Linking.addEventListener('url', this.handleNavigation);
      }

  }

Not working code following..



componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);
}

componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppStateChange);
  }

this.state.appState declared in constructor(props)

_handleAppStateChange = (nextAppState) => {
    if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
     console.log('App has come to the foreground!');debugger
 if (Platform.OS === 'android') {
          console.log("Testing");debugger

          //Constants.OneTimeFlag == false;
              Linking.getInitialURL().then(url => {
                console.log(url);
                var str = url
                var name = str.split('/')[4]
                Constants.isLinking = true;
               this.setState({ shop_Id: name})


               if (str)
               {
                this.setState({ isFromHomeLinking:'FROM_LINK' })
                this.props.navigation.navigate('SingleProductScreen', { ListViewClickItemHolder: [this.state.shop_Id,1,this.state.isFromHomeLinking] });

               }


              });

        }

        else {
            Linking.addEventListener('url', this.handleNavigation);
          }
    }
    }

当我在后台状态 Linking.getInitialURL() 中打开来自 whatsapp 和应用程序的外部链接时,接收为 null ..

以下是 list 文件

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
 <action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
 <category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http"
 android:host="demo1.zgroo.com" />
</intent-filter>
      </activity>

以下是我的示例 URL..

http://demo1.zgroo.com/xxxx

请让我知道任何解决方案..

提前致谢..

最佳答案

您需要为此案例注册 Linking 监听器。

componentDidMount() {
  Linking.addEventListener('url', this._handleOpenURL);
},
componentWillUnmount() {
  Linking.removeEventListener('url', this._handleOpenURL);
},
_handleOpenURL(event) {
  console.log(event.url);
}

更多https://facebook.github.io/react-native/docs/linking

关于android - 当应用程序处于后台状态 React native 时深度链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53256246/

相关文章:

android - 让 Android 应用程序对深层链接使用react

android - 在小米设备上运行 Monkey 测试返回异常

android - 为什么我的系统覆盖会捕获所有触摸(无法与底层 View 进行交互)

Android studio 在布局设计过程中卡住

javascript - 无法从 react 导航获得基本示例以正常工作

ios - Universal Link 打开错误的包 ID

Android Firebase 实时数据库替换数据而不是添加新数据

android - react native 访问内容 ://uri fails

react-native - 防止在 React Native 快速刷新时重置 Redux 状态(expo cli)

android - 推送通知中的深层链接问题