5

I've a React-Native app with the following code (here App.js, the app entry point) that manages deep link on Android.

Linking.getInitialURL().then((deepLinkUrl) => {
  if (deepLinkUrl) {
    manageDeepLink(deepLinkUrl);
  } else {
    Navigation.startSingleScreenApp('rootScreen');
  }
});

The problem here is that getInitialURL is called every time I launch my app, from both deep link or normally, and everytime it contains deepLinkUrl parameter empty. I've registered in AndroidManifest my intent as follows:

<application
    android:name=".MainApplication"
    android:allowBackup="true"
    android:launchMode="singleTask"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize">
        <!-- deeplink -->
        <intent-filter android:label="@string/app_name">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="myapp" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

UPDATE I'm using react-native navigation to register screens, if this can e useful.

4

2 回答 2

2

如果您在应用程序生命周期中过早注册侦听器似乎不起作用(例如,直接在某些 .js 文件中,因此在加载应用程序时执行)。

如果将它移到componentDidMount()根组件上,一切正常。

    componentDidMount() {
        Linking.addEventListener('url', event => {
            console.warn('URL', event.url)
        })

        Linking.getInitialURL().then(url => {
            console.warn('INITIAL', url)
        })
    }
于 2019-05-27T11:54:31.040 回答
0

因为您添加android:launchMode="singleTask"到 AndroidManifest.xml。请将其修改为android:launchMode="singleTop",然后您的问题将消失。

于 2021-01-14T09:16:43.830 回答